mardi 23 août 2016

SIGSEV SPOJ(PRIME1)

The problem I am solving is PRIME1 which says to print all the prime numbers between 2 numbers. Although the code I made runs properly on my compiler with the worst case as well, It gives me a SIGSEV error when compiling in SPOJ. Here is the code

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    int t;
    cin>>t;
    while(t>0)
    {
        int a,b,n,p;
        cin>>a>>b;
            n = b-a;
            bool prime[n+1];
            memset(prime,true,sizeof(prime));

            for(p=2;p*p<=b;p++)
            {
                  if(prime[p]==true)
                  {
                        for(int i=p*2;i<=b;i+=p)
                        {
                              prime[i]=false;
                        }
                  }
            }

            (a==1)?p=2:p=a;
            for(p;p<=b;p++)
                  if(prime[p])
                        cout<<p<<endl;
            t--;
    }
}

Aucun commentaire:

Enregistrer un commentaire