-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSieve.cpp
More file actions
39 lines (32 loc) · 701 Bytes
/
Sieve.cpp
File metadata and controls
39 lines (32 loc) · 701 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// Sieve of Eratosthenes, O(n*log(log(n)))
vector<bool> is_prime;
void sieve(int n) {
is_prime.assign(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 4; i <= n; i += 2) {
is_prime[i] = false;
}
for (int i = 3; i * i <= n; i += 2) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += 2 * i) {
is_prime[j] = false;
}
}
}
}
void solve() {
const int N = 1e9;
sieve(N);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}