chore(CPlusPlus): add segmented sieve for range (#535)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/582/head
Puneet Goel 2021-10-15 06:07:26 -07:00 committed by GitHub
parent 385027e60b
commit 228d6e3615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,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((RL+1)log(R) + R)
Input:
30 50
Output:
31 37 41 43 47
Input:
10 20
Output:
11 13 17 19
*/

View File

@ -140,6 +140,7 @@
- [Missing number](Maths/missing-number.cpp)
- [Factorial of a number](Maths/factorial.cpp)
- [Prime-number](Maths/prime-number.cpp)
- [Segmented Sieve](Maths/segmented-sieve-range.cpp)
# Recursion