1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public int countPrimes(int n) { boolean[] primes = new boolean[n]; Arrays.fill(primes, true); for (int i = 2; i * i < n; i++) { if (!primes[i]) { continue; } for (int j = i * i; j < n; j += i) { primes[j] = false; } }
int count = 0; for (int i = 2; i < primes.length; i++) { if (primes[i]) { count++; } } return count; } }
|