#include<stdio.h>
int main()
{
int t,m,n;
scanf("%d",&t); // number of test cases
while(t>0)
{
scanf("%d%d",&m,&n);
int prime[n]; // for finding prime numbers
int listofprime[n]; // for storing only prime numbers
int pos=0;
for(int i=2;i<=n;i++) //declare all to 1
prime[i]=1;
prime[0]=0;
prime[1]=0;
for(int i=2;i*i<=n;i++) //simple sieve to find primes till sqrt(n)
{
if(prime[i]==1)
{
listofprime[pos]=i;
pos++;
for(int mult=2;i*mult<=n;mult++)
prime[i*mult]=0;
}
}
for(int i=0;listofprime[i]*listofprime[i]<=n;i++) //segmented sieve
{
int low=m/listofprime[i];
low*=listofprime[i];
for(int j=low+listofprime[i];j<=n;j+=listofprime[i])
prime[j]=0;
}
for(int i=m;i<=n;i++) // print all prime numbers
{
if(prime[i]==1)
printf("%d\n",i);
}
t-=1;
}
}
#include<stdio.h>
int main()
{
int t,m,n;
scanf("%d",&t); // number of test cases
while(t>0)
{
scanf("%d%d",&m,&n);
int prime[n+1]; // for finding prime numbers
int listofprime[n+1]; // for storing only prime numbers
int pos=0;
for(int i=2;i<=n;i++) //declare all to 1
prime[i]=1;
prime[0]=0;
prime[1]=0;
for(int i=2;i*i<=n;i++) //simple sieve to find primes till sqrt(n)
{
if(prime[i]==1)
{
listofprime[pos]=i;
pos++;
for(int mult=2;i*mult<=n;mult++)
prime[i*mult]=0;
}
}
for(int i=0;listofprime[i]*listofprime[i]<=n;i++) //segmented sieve
{
int low=m/listofprime[i];
low*=listofprime[i];
for(int j=low+listofprime[i];j<=n;j+=listofprime[i])
prime[j]=0;
}
for(int i=m;i<=n;i++) // print all prime numbers
{
if(prime[i]==1)
printf("%d\n",i);
}
t-=1;
}
}
@suyashsing234
Remove lines 26 to 32 in your last code (the "segmented sieve" bit) and it will work. No idea what you are doing here: I suspect you are trying to do (wrongly) what you are already doing on lines 22 and 23. Your sieve has already correctly set up the array prime[] and I don't see why you now intend to change it.
#include<stdio.h>
int main()
{
int t,m,n;
scanf("%d",&t); // number of test cases
while(t>0)
{
scanf("%d%d",&m,&n);
int prime[n+1]; // for finding prime numbers
int listofprime[n+1]; // for storing only prime numbers
int pos=0;
for(int i=2;i<=n;i++) //declare all to 1
prime[i]=1;
prime[0]=0;
prime[1]=0;
for(int i=2;i<=n/i;i++) //simple sieve
{
if(prime[i]==1)
{
listofprime[pos]=i;
pos++;
for(int mult=2;i*mult<=n;mult++)
prime[i*mult]=0;
}
}
for(int i=m;i<=n;i++) // print all prime numbers
{
if(prime[i]==1)
printf("%d\n",i);
}
t-=1;
}
}