-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsegmentedSeivePrimeGenerator.cpp
64 lines (51 loc) · 1.11 KB
/
segmentedSeivePrimeGenerator.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
//22.08.2020
Hello world welcomes you to programming
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
void simpleSieve(lli limit, vector<lli>& prime)
{
bool mark[limit + 1];
memset(mark, false, sizeof(mark));
for (lli i = 2; i <= limit; ++i) {
if (mark[i] == false) {
prime.push_back(i);
for (lli j = i; j <= limit; j += i)
mark[j] = true;
}
}
}
void primesInRange(lli low, lli high)
{
lli limit = floor(sqrt(high)) + 1;
vector<lli> prime;
simpleSieve(limit, prime);
lli n = high - low + 1;
bool mark[n + 1];
memset(mark, false, sizeof(mark));
for (lli i = 0; i < prime.size(); i++) {
lli loLim = floor(low / prime[i]) * prime[i];
if (loLim < low)
loLim += prime[i];
if(loLim==prime[i])
loLim += prime[i];
for (lli j = loLim; j <= high; j += prime[i])
mark[j - low] = true;
}
for (lli i = low; i <= high; i++)
if (!mark[i - low])
cout << i << "\n";
}
int main()
{
lli low,t , high ;
cin>>t;
while(t--){
cin>>low>>high;
if(low == 1)
low ++;
primesInRange(low, high);
cout<<"\n";
}
return 0;
}