I have to create a program in which t
sets of m
and n
will be given by the user. For each set, we have to generate all prime numbers between m
and n
. This code is successfully executed with the correct output, but I have to reduce the time limit for a large data range.
#include<math.h>
#include<stdio.h>
int isPrime(int k);
int j;
int isPrime(int k)
{
for(j=2; j<=(int)sqrt(k); j++)
{
if(k%j==0)
return 0;
}
return 1;
}
int main()
{
int u,i,t,m,n,k;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &m, &n);
for(i=m;i<=n;i++)
{
u = isPrime(i);
if (u == 1)
printf("%d\n", i);
}
}
return 0;
}