-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88e0d9b
commit ab8511e
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|