This solution is the implementation of Sieve of Eratosthenes Algorithm. This is a well-known and efficient algorithm for finding all prime numbers up to a specified integer.
publicclassCountPrimes {
publicstaticintcountPrimes(int n) {
if (n <= 2) {
return 0;
}
boolean[] isPrime =newboolean[n];
for (int i = 2; i < n; i++) {
isPrime[i]=true;
}
for (int p = 2; p * p < n; p++) {
if (isPrime[p]) {
for (int i = p * p; i < n; i += p) {
isPrime[i]=false;
}
}
}
int primeCount = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i]) {
primeCount++;
}
}
return primeCount;
}
}