```
public class Solution {
public int countPrimes(int n) {
if (n < 3)
return 0;
int count = n / 2;
boolean[] isPrime = new boolean[n];
for (int i = 3; i * i < n; i += 2) {
if (isPrime[i])
continue;
for (int j = i * i; j < n; j += 2 * i) {
if (!isPrime[j]) {
--count;
isPrime[j] = true;
}
}
}
return count;
}
}
```