forked from codersinghal/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmentedSieve.cpp
More file actions
29 lines (28 loc) · 740 Bytes
/
segmentedSieve.cpp
File metadata and controls
29 lines (28 loc) · 740 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
/**
* Description: Get primes in range [ll, ul].
* Usage: getPrimes. O(NlgN) where N = ul-ll+1
* Source: https://github.com/dragonslayerx
*/
void getPrimes(int ll, int ul, set<int> &largePrimes) {
vector<bool> isprm;
isprm.resize(ul - ll + 1);
for(int i = 0; i < ul - ll + 1; i++) {
isprm[i] = 1;
}
for (int i = 2; i * i <= ul; i++) {
if (isprime[i]) {
int j;
j = ll / i;
for(; i * j <= ul; j++) {
if (i * j >= ll && j > 1) {
isprm[i * j - ll] = 0;
}
}
}
}
for (int i = ll; i <= ul; i++) {
if (isprm[i - ll] && i > 1) {
largePrimes.insert(i);
}
}
}