vendredi 22 mars 2019

My solution for Prime Generator(SPOJ) is not accepted because time limit exceeds

The Prime Generator requires prime numbers between a certain range and has certain test cases to check that. My program runs perfect with this solution but the time limit is exceeded and it isn't accepted as a solution.

I've replaced cin and cout with scanf and printf. I've replaced for loops with while loops and what not.

#include<iostream>
int prime(unsigned long int p)
{ 
int f=1,i=2;
while(i<=p/2)
{
if(p%i==0)
{f=0;   break;
}
++i;
}
if(f==1)
{printf("%d \n",p);
}
return 0;
}

int main()
{
int t,f=0,i=0;
unsigned long int m,n,j;
scanf("%d",&t);
while(i<t)
{
scanf("%lu%lu",&m,&n);
for(j=m;j<=n;++j)
{
if(j!=1&&j!=0)
prime(j);
}
printf("\n");
++i;
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire