-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegmented_sieve.cpp
113 lines (79 loc) · 1.78 KB
/
segmented_sieve.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/Peter wants to generate some prime numbers for his cryptosystem.
Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10).
In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Example
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
*/
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
vector <int> primes; // storing prime numbers
void segmentedSieve(int l, int r) {
if (l == 1) l++; // one is not prime
int maxN = r - l + 1;
vector <bool> seg(maxN, true);
for (auto &p: primes) {
if (p * p <= r) {
// first multiple of p in [l, r]
int i = (l / p) * p;
if (i < l) i += p;
for (; i <= r; i += p) {
if (i != p) {
seg[i - l] = false;
}
}
} else break;
}
// print primes in [l, r]
for (int i = 0; i < maxN; i++) {
if (seg[i] == true) {
cout << l + i << "\n";
}
}
}
void sieveOfEratosthenes (vector <bool> &sieve) {
for (int i = 2; i * i <= MAX; i++) {
if (sieve[i] == true) {
for (int j = i * i; j <= MAX; j += i) {
sieve[j] = false;
}
}
}
for (int i = 2; i <= MAX; i++) {
if (sieve[i] == true) {
primes.push_back(i);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
// will take l, r range
// generate prime numbers till sqrt(r)
// r -> 10^9
vector <bool> sieve(MAX + 1, true);
sieveOfEratosthenes(sieve);
while (t--) {
int l, r;
cin >> l >> r;
segmentedSieve(l, r);
cout << "\n";
}
return 0;
}