MATLAB: I dont what is the problem in this simple program plz help distinguish prime numbers

help prime

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

Best Answer

  • 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. :-(