I am getting a run-time error whenever I run the code on spoj platform.The code first creates an array containing first s numbers in r[].Since r[0 and 1 are not prime,I assign it as zero.Then I used the
sieve of eratosthenes algorithm to create a array p[c] of prime numbers.Then for the input m and n I iterated to check whether the number is divisible by all the primes below the square root of num.If it is then it prints the prime numbers. Please tell where the error is.
Thanks in Advance!!
#include<stdio.h>
#include<math.h>
int main()
{
int t,m,n,i,temp,flag,j; /declare variables/
scanf("%d",&t);
unsigned int s=1000000000;
long int r[s+1];
r[0]=0;
r[1]=0;
for(i=2;i<=s;i++)
{
r[i]=i;
}
for(i=2;i<=s;i++)
{
if(r[i]!=0)
{
for(j=2*i;j<=s;j=j+i)
{
r[j]=0;
}
}
}
int c;
c=0;
for(i=2;i<=s;i++)
{
if(r[i]!=0)
{
c++;
}
}
unsigned int p[c];
c=0;
for(i=2;i<=s;i++)
{
if(r[i]!=0)
{
p[c]=i;
/printf("\np[%d] is %d",c,p[c]);/
c++;
}
}
while(t–)
{
scanf("%d%d",&m,&n);
if(m==1)
{
m++;
}
for(;m<=n;m++)
{
flag=0;
temp=sqrt(m);
for(i=0;p[i]<=temp;i++)
{
if(m%p[i]==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%u\n",m);
}
}
}
return 0;
}