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 );
}
Chat with our AI personalities