Count Primes
Using sieve method. Have a look at the sample code below and note that:
- All even integer except 2 is not prime.
- Pay attention to the start position and increment of sieving loop (the loop against
j
). - We can use a
BitSet
to save a lot of space. - One is not prime.
More instructions at LeetCode's problem hint for this problem.
public class CountPrimes {
public int countPrimes(int n) {
BitSet set = new BitSet(n);
if (n <= 2) return 0;
int count = 1;
for (int i = 3; i < n; i += 2) {
if (set.get(i)) continue;
count++;
for (long j = i *(long)i; j < n; j += 2 * i) {
set.set((int)j);
}
}
return count;
}
}