How to determine if a number is prime?

98 vues (au cours des 30 derniers jours)
Juan Zegarra
Juan Zegarra le 1 Mai 2019
Hello, I was wondering if you can help how to determine if numbers from 0 to 100 are prime. Should I use loops? Please I am really confused with this homework.
  2 commentaires
Rik
Rik le 1 Mai 2019
There are many ways you could solve this. What was the exact assignment? I suspect you're not allowed to use the isprime function.
How would you solve this on paper? That's usually a good start for how to solve it in any programming language.
You can find guidelines for posting homework on this forum here (and there is also a lot of helpful advice on that page).
Raj
Raj le 2 Mai 2019
Modifié(e) : Raj le 2 Mai 2019

Connectez-vous pour commenter.

Réponse acceptée

jahanzaib ahmad
jahanzaib ahmad le 2 Mai 2019
Modifié(e) : jahanzaib ahmad le 2 Mai 2019
thats not difficult .try to solve it on paper first .for example u have a number 100 . how will u check that its prime or not ?
divide it with all numbers from 1 to 99 .. and if any time the remainder is zero its not a prime number
to divide 100 from 1 to 100 u can use for loop .
  1 commentaire
Rik
Rik le 2 Mai 2019
As is probably mentioned in the links posted above, you don't need to check up to 99, checking up to the square root of your number (and exiting the loop when you found a factor) will get you a big jump in performance.
An even better method would be to write a prime number sieve (use ismember to find the multiples).

Connectez-vous pour commenter.

Plus de réponses (1)

Ozkan
Ozkan le 8 Mai 2023
% First n terms of Fibonacci series
n = 55;
% Starting with the first two terms are 1 and 1
fibo = [1, 1];
% Calculate the remaining terms and add them into the serie
for i = 3:n
fibo(i) = fibo(i-1) + fibo(i-2);
end
% Create a pointer vector to point the prime numbers
prime_flags = false(size(fibo));
% Check if the term in the serie is a prime number
for i = 1:n
% Prime numbers start from 2, thus no need to check the first two terms
if i > 2
% Check each number
for j = 2:sqrt(fibo(i))
% If division has no remainder, it is not a prime number
if rem(fibo(i), j) == 0
break;
end
end
% If there is no integer divider then it is a prime number
if rem(fibo(i), j) ~= 0
prime_flags(i) = true;
end
end
end
% find the indices of prime flags
prime_indices = find(prime_flags);
disp(fibo);
1.0e+11 * Columns 1 through 19 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 20 through 38 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0001 0.0002 0.0004 Columns 39 through 55 0.0006 0.0010 0.0017 0.0027 0.0043 0.0070 0.0113 0.0184 0.0297 0.0481 0.0778 0.1259 0.2037 0.3295 0.5332 0.8627 1.3958
disp(prime_indices)
5 7 11 13 17 23 29 43 47

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by