parent nodes: MatematicasApuntes
TestsPrimality
Author: César TM
Date: 09/09/2009
A Function that TestsPrimality
Copiada de "Programming with C++ 2nd ed" (Schaum) McGraw-Hillbool isPrime( unsigned int n )
{ // returns true if n is prime, false otherwise:
if( n < 2 )
return false; // 0 and 1 are not primes
if( n < 4 )
return true; // 2 and 3 are the first primes
if( n%2 == 0 )
return false; // 2 is the only even prime
float sqrtn = sqrt( n ); // pasar a (unsigned int) para no comparar "integer <= float" en el for
for( int d = 3; d <= sqrtn; d += 2 )
if( n%d == 0 )
return false; // n has a nontrivial divisor
return true; // n has no nontrivial divisors
}
_________________________________________________________________________________________________________
MatematicasApuntes [camelCaseWordsEnabled: false]