i dont what is the problem in this simple program plz help distinguish prime numbers

n=input ('the num ');
if n==1 ;
disp('the num is prime ')
break
else
for i=n:sqrt(n);
if rem(n,i)==0;
disp('the num is not prime ')
else
disp('the num is prime')
end
end
end

 Akzeptierte Antwort

the cyclist
the cyclist am 20 Dez. 2013
Bearbeitet: the cyclist am 20 Dez. 2013
Two errors I see:
Your "break" statement should be a "return" statement.
Where you have
for i=n:sqrt(n)
you should have
for i=2:sqrt(n)
I think.
When you make those changes, you actually get into your for loop properly, and you will then see that your algorithm is flawed. :-(

1 Kommentar

ok thnx there is another problem but not technical its in the idea anyway i made another i find another successful program num=input('the num '); j=0 if num~=0 for i=2:sqrt(m); if rem(n,i)==0; j=j+1; end end if j~=0; disp('the num is not prime'); else disp('the num is prime') end else('num is prime') end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

if isprime(num)
disp('The num is prime')
else
disp('The num is not prime')
end

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by