-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsieve.cpp
More file actions
69 lines (44 loc) · 1.05 KB
/
sieve.cpp
File metadata and controls
69 lines (44 loc) · 1.05 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
// tc -> O(nlog(logn))
int N = 1000000;
bool sieve[1000001];
void createSieve() {
for (int i = 2; i <= N; i++) {
sieve[i] = true;
}
for (int i = 2; i <= N; i++) { // i*i<=N
if (sieve[i] == true) { // to marks it multiples
for (int j = 2 * i; j <= N; j += i) { // j = i*i this first optimization we do
sieve[i] = false;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
//input.txt
freopen("input1.txt", "r", stdin);
//output.txt
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
// sieve of eratosthenes
// some who is false will not mark its multiples as someone else would have already mark its multiples-> the black box
// #precompute
// tc -> O(nlog(logn))
createSieve();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (sieve[n] == true) {
cout << "TRUE" << endl;
}
else {
cout << "NO" << endl;
}
}
}