PRIME1 WA....

#include<stdio.h>
#include<math.h>
int isprime(unsigned long x)
{
int i,c=0;
if(x==1)
return 0;
else if(x==2 || x==3)
return 1;
else if(x%2==0)
return 0;
else if(x%3==0)
return 0;
else
{
for(i=5;(i<=sqrt(x))&&((i%3)!=0);i+=2)
{
if(x%i==0)
c++;
}
if(c==0)
return 1;
else
return 0;
}
}

int main()
{
    int t,i,j;
    unsigned long a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lu%lu",&a,&b);
        for(i=a;i<=b;i++)
        {
            if(isprime(i)==1)
                printf("%lu\n",i);
        }
        printf("\n");
   }
   return 0;
}

Hello,

The isPrime method is very, very naive. The loop will only run two times: for i=5 and i=7, when i reaches 9 the loop will break because of the condition (i%3)!=0. You should keep trying. The method needs a LOT of improvement (I will avoid giving advices because you asked why it is giving wrong answer, if you need help solving the problem or any tips just ask I would gladly help but I strongly recommend to try and solve it on your own). Look well at the constraints of the problem, think carefully about how you’re approaching the problem. I think there are several ways to solve it but there are approaches that will be really hard to implement in order to run in the given time for the given constraints.

5 Likes

I like the part: “I will avoid giving advices because you asked why it is giving wrong answer…”, this is the way I think is correct :wink:

1 Like

+1 for the precise answer that is seen rarely these days on the forum.

1 Like

Thanks @betlista and @bugkiller

//