A program to test for primality without using isprime() or factor()?

  • Follow


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!
0
Reply Oli 1/25/2011 10:26:05 AM

"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 if-condition 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
0
Reply Jos 1/25/2011 10:36:04 AM


"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!
try this
%-----
function primalitytest = primality(n)
n = input('Enter value of n: ');
m = 2; % initialise factor to test
t=floor(sqrt(n));count=0;
for m = 2:t
    if mod(n,m) == 0 %m is a factor of n
        disp('n is not prime')
        primalitytest=0;
    else 
        count=count+1;
    end
end;
if count==t-1
    disp(' n is prime');
    primalitytest=1;
end
%-------
0
Reply Krishna 1/25/2011 10:44:07 AM

Thanks very much

"Krishna Kumar" <skrishnakumar.accet@gmail.com> wrote in message <ihm9hn$lsr$1@fred.mathworks.com>...
> "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!
> try this
> %-----
> function primalitytest = primality(n)
> n = input('Enter value of n: ');
> m = 2; % initialise factor to test
> t=floor(sqrt(n));count=0;
> for m = 2:t
>     if mod(n,m) == 0 %m is a factor of n
>         disp('n is not prime')
>         primalitytest=0;
>     else 
>         count=count+1;
>     end
> end;
> if count==t-1
>     disp(' n is prime');
>     primalitytest=1;
> end
> %-------
0
Reply Oli 1/25/2011 11:52:03 AM

3 Replies
553 Views

(page loaded in 0.043 seconds)

Similiar Articles:






7/22/2012 7:52:09 AM


Reply: