"Oli " <ow223@cam.ac.uk> wrote in message <ihm8ft$eid$1@fred.mathworks.com>...
> Hi, I need to make a program that deduces if a number is prime or not without using the built in functions isprime() and factor(). I've managed to come up with the following that tells me when a number is not prime, but doesn't say anything when a number is prime. I just need help altering it so that if n is prime, it states 'n is prime'....
>
> function primalitytest = primality(n)
> n = input('Enter value of n: ')
> m = 2; % initialise factor to test
> for m = 2:floor(sqrt(n))
> if mod(n,m) == 0 %m is a factor of n
> disp('n is not prime')
> end
> end;
>
> Any help would be much appreciated!
One option: if the ifcondition is true you would like to RETURN immediately without having the rest of the loop being run. If the loop is finished completely, then you can DISPlay the text "N is prime".
In pseudocode:
for ...
if ...
disp "no" & return
end
end
disp "yes"
hth
Jos
