A prime number is a positive integer greater than one with exactly two positive divisors: one and itself. section 3.3.1
A practical primality check handles 2 and 3 directly, rejects values at most 1 and multiples of 2 or 3, then tests possible factors from 5 through the square root. section 3.3.1
Random prime guessing game
Generate integers from 1 to 99,999 and keep rejecting candidates until is_prime accepts one. Use the result as the target in a guessing game that reports whether guesses are too small or too large. section 3.3 section 3.3.3
Listing 3.11 Function to check whether a number is prime
bool is_prime(int n)
{
if (n == 2 || n == 3) #1
return true;
if (n <= 1 || n % 2 == 0 || n % 3 == 0) #2
return false;
for (int i = 5; i * i <= n; ++i) #3
{
if (n % i == 0)
return false; #4
}
return true; #5
}
Inspect the complete C++ primality-checking function, including modulo tests, the square-root stopping condition, and early returns. section 3.3.1




