-2

I tried a prime no generating question in SPOJ(link : http://www.spoj.com/problems/PRIME1/) . I'm using seive algorithm . I get the SIGSEGV error when i use spoj gcc . But when i compiled using my ubuntu gcc it works for all the test cases.

Here is my source code . Plz Help

float sqroot( float x)
{ 
    float a , b;
    a = x; // copy given value to 'a'
    do
    {
        b = a; // copy value of 'a' to 'b' before 'a' is modify
        a = (a + x/a) / 2; // modify 'a' value until we reach sqroot result
    }
    while( a!= b); // execute loop until a == b
    return( a); // 'a ' or 'b' is sqroot of 'x'
}
int main()
{
    int prime[4000];
    int prime_index=0;
    bool find_prime[100001];
    int i,j;
    int m,n;
    int iremainder;
    int T,t_index;
    int PRIME_FLAG=1;
    float square;
    int limit;
    prime_index++;
    prime[prime_index]=2;
    for(i=3;i<=32000;i=i+2)
    {
        PRIME_FLAG=1;
        square = sqroot((float)i);
        limit = ((int)(square))+1;
        for(j=1;j<=prime_index,prime[j]<=limit;j++)
        {
            if(prime[j]!=0)
            {
                if((i%prime[j]) == 0)
                {
                    PRIME_FLAG = 0;
                    break;
                }
            }
        }
        if(PRIME_FLAG)
        {
            prime_index++;
            prime[prime_index]=i;
            printf("%d\n",i);
        }
    }
    printf("Enter the no of test cases:");
    scanf("%d",&T);
    if(T<=10)
    {
        for(t_index=1;t_index<=T;t_index++)
        {
            printf("Enter the values of m and n :");
            scanf("%d%d",&m,&n);
            if((m>=1) && (n<=1000000000) && ((n-m)<=100000))
            {
                if(m == 1)
                    m=2;

                //Set all numbers from m to n as prime 
                for(i=m;i<=n;i++)
                    find_prime[i]=true;
                //Find the prime numbers between m to n
                square = sqroot((float)n);
                limit = ((int)(square))+1;
                for(i=1;i<=prime_index,prime[i]<=limit;i++)
                {
                    if(m>=prime[i])
                    {
                        if(prime[i]!=0)
                            iremainder=m%prime[i];
                        j=prime[i]*iremainder;
                    }
                    else
                    {
                        iremainder=prime[i]-m;
                        if(m+iremainder == prime[i])
                            j=2*(m+iremainder);
                        else
                            j=m+iremainder;
                    }
                    for(;j<=n;j=j+prime[i])
                        find_prime[j]=false;
                }
                //Print all prime no's
                for(i=m;i<=n;i++)
                {
                    if(find_prime[i])
                        printf("%d\n",i);
                }
            } 
        }
    }
    return 0;
}
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
user3045438
  • 45
  • 1
  • 4

1 Answers1

1

your for loops are wrong.

for(j=1;j<=prime_index,prime[j]<=limit;j++)

should be

for(j=1;(j<=prime_index)&&(prime[j]<=limit);j++)

the first condition will be executed, but the result will be ignored.

So it is luck, that you find a number in the uninitialized array that is larger than limit before you go out of range of the array, which will lead to a SIGSEGV.

for(i=1;i<=prime_index,prime[i]<=limit;i++)

has the same problem.

Click here for more details

Community
  • 1
  • 1
mch
  • 9,424
  • 2
  • 28
  • 42