Skip to content

Commit

Permalink
Problem PRIME1 from SPOJ
Browse files Browse the repository at this point in the history
  • Loading branch information
mani1996 committed Oct 5, 2016
1 parent 6d496b9 commit 502f7db
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions PRIME1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Problem: PRIME1 from SPOJ

#include <bits/stdc++.h>
using namespace std;

vector<int> primes;
bool isPrime[40001]; // use this to answer queries if n <= 40000

void sieve(){
int i,j;

for(i=1;i<=40000;i++)isPrime[i] = true;
isPrime[1] = false;

for(i=2;i<=200;i++){
if(isPrime[i]){
primes.push_back(i);
for(j=2*i;j<=40000;j+=i)isPrime[j] = false;
}
}

for(i=201;i<=40000;i++)if(isPrime[i])primes.push_back(i);

return ;
}

void solve(){
int m,n,i,c;
cin>>m>>n;

bool prime[n-m+1];
for(i=0;i<n-m+1;i++)prime[i] = true;

for(i=0;i<primes.size();i++){
c = m%primes[i];
if(c!=0)c = primes[i]-c;

while(c<n-m+1){
prime[c] = false;
c+=primes[i];
}
}

for(i=0;i<n-m+1;i++){
if(m+i<=40000){
if(isPrime[m+i])cout<<(m+i)<<"\n";
}
else{
if(prime[i])cout<<(m+i)<<"\n";
}
}

cout<<"\n";
return ;
}

int main(){
int t;
cin>>t;

sieve();

while(t--){
solve();
}
}

0 comments on commit 502f7db

Please sign in to comment.