-Eratoshenes(厄拉托塞师)筛法-埃氏筛
class Solution {
public int countPrimes(int n) {
boolean[] isPrime = new boolean[n];
Arrays.fill(isPrime, true);
int cnt = 0;
for(int i = 2; i < n;i++) {
if(isPrime[i]) {
cnt++;
//防int溢出
if((long)i * i < n)
for(int j = i * i;j < n;j += i) {
isPrime[j] = false;
}
}
}
return cnt;
}
}
注意要防i过大时int溢出,if((long)i * i < n)