CIN010-Editorial

PROBLEM LINK:CodeChef: Practical coding for everyone

Author: Rajan Arora
Tester: Rajan Arora
Editorialist: Rajan Arora

DIFFICULTY:

EASY

PREREQUISITES:

Prime Sieve, Segmented Prime Sieve

PROBLEM:

Rohit is developing a hash function for his custom hashmap. He wants to find all the prime numbers between two given numbers so that he can use them as a key for his custom hashmap. Since he is busy with some other task, Help him to find all the prime numbers between two given numbers.

QUICK EXPLANATION:

here we cannot use a prime sieve as it would give a memory error, so instead we use a segmented prime sieve.

EXPLANATION:

Problems with Simple Sieve:
The Sieve of Eratosthenes looks good, but consider the situation when n is large, the Simple Sieve faces following issues.

  • An array of size Θ(n) may not fit in memory
  • The simple Sieve is not cache friendly even for slightly bigger n. The algorithm traverses the array without locality of reference

Segmented Sieve
The idea of segmented sieve is to divide the range [0…n-1] in different segments and compute primes in all segments one by one. This algorithm first uses Simple Sieve to find primes smaller than or equal to √(n). Below are steps used in Segmented Sieve.

  1. Use Simple Sieve to find all primes upto square root of ‘n’ and store these primes in an array “prime[]”. Store the found primes in an array ‘prime[]’.
  2. We need all primes in range [0…n-1]. We divide this range in different segments such that size of every segment is at-most √n
  3. Do following for every segment [low…high]
  • Create an array mark[high-low+1]. Here we need only O(x) space where x is number of elements in given range.
  • Iterate through all primes found in step 1. For every prime, mark its multiples in given range [low…high].

In Simple Sieve, we needed O(n) space which may not be feasible for large n. Here we need O(√n) space and we process smaller ranges at a time (locality of reference)

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
const lli mod=1e9+7;
const lli mod1=998244353;
lli vis[1000001];
lli vis1[1000001];
int main()
{
lli T;
cin>>T;
lli x,y;
lli N=1000000001;
for(x=2;xx<=N;x++)
{
if(vis1[x]==false)
{
for(y=x
x;yy<=N;y+=x)
{
vis1[y]=true;
}
}
}
while(T–)
{
lli n,m,i;
cin>>n>>m;
for(i=2;i
i<=m;i++)
{
if(vis1[i]==false)
{
lli j=(n/i)*i;
if(j<n)
j+=i;
if(j==i) j+=i;
for(;j<=m;j+=i)
{
vis[j-n]=true;
}
}
}
lli cnt=0;
for(i=0;i<=(m-n);i++)
{
if((vis[i]==false)&&(i+n!=1)) cout<<i+n<<" ";
}
for(i=0;i<=(m-n);i++)
vis[i]=false;
cout<<endl;

}
return 0;

}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
const lli mod=1e9+7;
const lli mod1=998244353;
lli vis[1000001];
lli vis1[1000001];
int main()
{
lli T;
cin>>T;
lli x,y;
lli N=1000000001;
for(x=2;xx<=N;x++)
{
if(vis1[x]==false)
{
for(y=x
x;yy<=N;y+=x)
{
vis1[y]=true;
}
}
}
while(T–)
{
lli n,m,i;
cin>>n>>m;
for(i=2;i
i<=m;i++)
{
if(vis1[i]==false)
{
lli j=(n/i)*i;
if(j<n)
j+=i;
if(j==i) j+=i;
for(;j<=m;j+=i)
{
vis[j-n]=true;
}
}
}
lli cnt=0;
for(i=0;i<=(m-n);i++)
{
if((vis[i]==false)&&(i+n!=1)) cout<<i+n<<" ";
}
for(i=0;i<=(m-n);i++)
vis[i]=false;
cout<<endl;

}
return 0;

}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
const lli mod=1e9+7;
const lli mod1=998244353;
lli vis[1000001];
lli vis1[1000001];
int main()
{
lli T;
cin>>T;
lli x,y;
lli N=1000000001;
for(x=2;xx<=N;x++)
{
if(vis1[x]==false)
{
for(y=x
x;yy<=N;y+=x)
{
vis1[y]=true;
}
}
}
while(T–)
{
lli n,m,i;
cin>>n>>m;
for(i=2;i
i<=m;i++)
{
if(vis1[i]==false)
{
lli j=(n/i)*i;
if(j<n)
j+=i;
if(j==i) j+=i;
for(;j<=m;j+=i)
{
vis[j-n]=true;
}
}
}
lli cnt=0;
for(i=0;i<=(m-n);i++)
{
if((vis[i]==false)&&(i+n!=1)) cout<<i+n<<" ";
}
for(i=0;i<=(m-n);i++)
vis[i]=false;
cout<<endl;

}
return 0;

}