answersLogoWhite

0

Testing c plus plus code prime number?

Updated: 8/17/2019
User Avatar

Wiki User

10y ago

Best Answer

Use the following function to determine if a whole number is prime, including negative values:

bool isprime(int p)

{

if(p<0)

p*=(-1); // convert to positive integer

if( p<2 ( p>2 && p&1==0 ))

return( false );

int max = (int)sqrt((double) p)+1;

for( int i=3; i<=max; i+=2 )

if( p%i==0 )

return( false );

return( true );

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Testing c plus plus code prime number?
Write your answer...
Submit
Still have questions?
magnify glass
imp