/* isPrime-fn2.h * efficient recognizer (predicate function) for prime numbers */ #include //for sqrt(), ceil() using namespace std; /* isPrime()--------------------------------------- * returns true if n is prime; otherwise returns false */ bool isPrime(unsigned long n){ unsigned long pd = 2, //potential divisor limit = sqrt(n); //upper bound for potential divisors int divisible = 0; //boolean flag (0 is false, 1 is true) //check divisors up to limit do{ divisible = (n % pd == 0); pd++; }while(!divisible && (pd <= limit)); //return result //if(divisible) //return 0; //else // return 1; return (!divisible || n == 2); }