How to determine if a number is prime?

121 visualizzazioni (ultimi 30 giorni)
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 Commenti
Rik
Rik il 1 Mag 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 il 2 Mag 2019
Modificato: Raj il 2 Mag 2019

Accedi per commentare.

Risposta accettata

jahanzaib ahmad
jahanzaib ahmad il 2 Mag 2019
Modificato: jahanzaib ahmad il 2 Mag 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 Commento
Rik
Rik il 2 Mag 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).

Accedi per commentare.

Più risposte (1)

Ozkan
Ozkan il 8 Mag 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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by