-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmented-sieve-range.cpp
84 lines (63 loc) · 1.59 KB
/
segmented-sieve-range.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
/*
Problem: finding all prime numbers in a range [L,R], where R <= 1e12 and (R-L+1) <= 1e7.
*/
#include <bits/stdc++.h>
using namespace std;
//function to find prime numbers between L and R
void segmented_sieve_range(long int L, long int R){
// generate all primes up to sqrt(R)
long int e = sqrt(R);
// vector to store whether ith element is visited or not
vector<bool> check(e + 1, false);
//stores all primes till sqrt(R)
vector<long int> primes;
for (long int i = 2; i <= e; ++i) {
if (!check[i]) {
//i is a prime number
primes.push_back(i);
for (long int j = i*i; j <= e; j += i){
check[j] = true;
}
}
}
vector<bool> isPrime(R - L + 1, true);
//edge case
if(L == 1){
isPrime[0] = false;
}
for (long int i = 0; i < primes.size() ; i++){
//current Prime
long int cur = primes[i];
long int product = cur*cur;
for(long int start = max(product, (L + cur -1)/product); start <= R ; start += cur){
isPrime[start - L] = false;
}
}
//Printing all primes in the range L to R
for(long int i = 0; i < isPrime.size(); i++){
if(isPrime[i]){
//current index element is a prime number
cout<< L + i << " ";
}
}
cout << endl;
}
//main starts
int main()
{
long int L, R;
cin >> L >> R;
segmented_sieve_range(L, R);
return 0;
}
/*
Time Complexity - O((R−L+1)log(R) + √R)
Input:
30 50
Output:
31 37 41 43 47
Input:
10 20
Output:
11 13 17 19
*/