5 / 5
May 2018

#include<stdio.h>

void main()

{

int a,b,i,p,k,t,n;

scanf("%d",&n);
for(t=1;t<=n;t++)
{

    scanf("%d%d",&a,&b);



 for(p=a;p<=b;p++)

{

 for(i=1,k=0;i<=p;i++)

  {
    
if(p%i==0)

     k++;

 }

if(k==2)

printf("%d ",p);

}
printf("\n");
}

}

  • created

    May '18
  • last reply

    May '18
  • 4

    replies

  • 811

    views

  • 3

    users

what for?
The program is perfect as far as the generation of prime numbers is concerned.

You don’t need to count the number of divisors, just to say whether there are any. So, to save time:

  • Only test up to square root as karanjakhar already said,
  • stop when you find the first divisor,
  • the only even number you need to test divisibility by is 2,
  • There’s no point in testing divisibility by 1.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 22 13d

Want to read more? Browse other topics in C and C++ or view latest topics.