Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true .
vector < bool > prime ( max_val + 1 , true ) ;