Preprocess all primes till MAX . Create a boolean array " isPrime [ 0 . . MAX ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true .
bool isPrime [ MAX + 1 ] ; memset ( isPrime , true , sizeof isPrime ) ; sieveOfEratosthenes ( isPrime ) ;