I am trying to solve this and i came up with this
#include <stdio.h>
#define primeLimit 100000
int prime (long int Start2, long int Stop2 )
{
long int a[primeLimit];
long int i,j,k,l;
for (i=Start2;i<=Stop2;i++) {
a[i] = 1;
}
for (i=Start2;i<=Stop2;i++) {
if (a[i]!= 0 && i!=1) {
for (j=2; j*j< i;j++) {
if(i%j==0)
break;
}
if(j*j > i) {
printf(" \n %d",i);
l = i;
for (k = i*i; k< Stop2;) {
a[k] = 0;
k = k+l;
}
}
else {
a[i] = 0;
}
}
}
return 0;
}
int main(void)
{
long int start,stop,a,look;
scanf("%ld", &look);
for (a=1;a<=look;a++) {
scanf("%ld %ld", &start,&stop);
prime (start,stop);
}
return 0;
}
I am using GNU GCC compiler, CodeBlocks IDE on 32 bit windows 7 PC. The code was fine when i ran it on my compiler. But when submitted it shows runtime error (SIGSEGV) .Can anyone point me out where i am going wrong/how to do it better?
Thank you
The allowed inputs range from 1 to 1000000000, but your array only has 100000 elements .. the difference between the two inputs. You should be indexing it with
i-Start2ork-Start2, notiork. And you needa[primeLimit+1], because you need to accessa[Stop2-Start2], i.e.,a[100000].