思路:
Sieve of Eratosthenes. https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
int countPrimes(int n) {
bool isPrime[n + 1];
memset(isPrime, true, sizeof(bool) * (n + 1));
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i < n; i++) {
if (isPrime[i]) {
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
}
}
int count = 0;
for (int i = 2; i < n; i++)
if (isPrime[i])
count++;
return count;
}