Skip to content

Commit

Permalink
add missing segmented sieve
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Feb 27, 2024
1 parent 88e0d9b commit ab8511e
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions theory/prime_and_factorisation/segmented_sieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <vector>

#define N 100000

typedef long long ll;

int sieveArr[N+1]={0};
std::vector<int> primes;


void sieve(){

for(ll i = 2; i <= N; ++i)
if(sieveArr[i] == 0) {
primes.push_back(i);
for(ll j = i*i; j<= N; j+=i)
sieveArr[j] = 1;
}

}


int main() {

// Precompute the sieve
sieve();

int t;
std::cin >> t;

while(t--) {
int a,b;
std::cin >> a >> b;

std::vector<int> segmented(b-a+1, 0);

for(auto p : primes) {
// stop when p^2 > n
if(p*p > b)
break;

// finding the nearest starting point
int start = (a/p)*p;

// special case
if(p>=a and p<=b)
start = 2*p;


for(int j = start; j <= b; j+=p) {
if(j < a)
continue;
// not prime
segmented[j-a] = 1;
}
}

// primes stored as 0 in the segment
for(int i = a; i <= b; ++i)
if(segmented[i-a] == 0 && i != 1)
std::cout << i << std::endl;
}

std::cout << std::endl;

return EXIT_SUCCESS;
}



0 comments on commit ab8511e

Please sign in to comment.