You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//欧拉筛,素数筛
class Solution {
public int countPrimes(int n) {
int ans = 0;
boolean[] np = new boolean[n + 1];
List<Integer> primes = new LinkedList<>();
for (int i = 2; i < n; ++i) {
if (!np[i]) {
primes.add(i);
}
for (int p : primes) {
if (p * i > n) {
break;
}
np[p * i] = true;
if (i % p == 0) {
break;
}
}
}
for (int i = 2; i < n; ++i) {
if (!np[i]) {
++ans;
}
}
return ans;
}
}
Count the number of prime numbers less than a non-negative number,
n
.Example 1:
Example 2:
Example 3:
Constraints:
0 <= n <= 5 * 106
对于 判断素数(prime number) 问题,一般使用的是素数筛法,第一种是常用的 埃拉托色尼筛法,时间复杂度是O(nlognlogn):
但该方法有个问题,一些数会被重复判定,比如12 = 2 * 6 = 3 * 4,所以引出如下欧拉筛法,也叫线性筛。
第二种是O(n)的 欧拉筛法,维护一个质数表(primes),每次用素数乘以质数表的数,排除那些非素数,并且循环是需要遍历所有数,而不是到Math.sqrt(n)为止。该筛法的核心思想是让每一个合数被其最小质因数筛到。
参考资料:
The text was updated successfully, but these errors were encountered: