Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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

From: Oli

Date: 25 Jan, 2011 10:26:05

Message: 1 of 4

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!

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

From: Jos (10584)

Date: 25 Jan, 2011 10:36:04

Message: 2 of 4

"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

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

From: Krishna Kumar

Date: 25 Jan, 2011 10:44:07

Message: 3 of 4

"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
%-------

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

From: Oli

Date: 25 Jan, 2011 11:52:03

Message: 4 of 4

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
> %-------

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us