-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountPrimes.cpp
More file actions
40 lines (39 loc) · 1.07 KB
/
countPrimes.cpp
File metadata and controls
40 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int countPrimes(int n) {
if (n < 3) return 0;
vector<bool> prime(n, true);
int ans = 0;
for (int i = 2; i < n; i++) {
if (!prime[i]) continue;
for (int j = 2 * i; j < n; j += i) {
if (prime[j]) {
prime[j] = false;
ans++;
}
}
}
return n-ans-2;
}
};
// Optimize
class Solution {
public:
int countPrimes(int n) {
if (n <= 2) return 0;
vector<bool> prime(n, true);
int i = 3, sqrtn = sqrt(n), count = n / 2; // 偶数一定不是质数
while (i <= sqrtn) { // 最小质因子一定小于等于开方数
for (int j = i * i; j < n; j += 2 * i) { // 避免偶数和重复遍历
if (prime[j]) {
--count;
prime[j] = false;
}
}
do {
i += 2;
} while (i <= sqrtn && !prime[i]); // 避免偶数和重复遍历
}
return count;
}
};