You are not logged in. Please login at www.codechef.com to post your questions!

×

SPOJ- PRIME GENERATOR WHY THIS CODE IS SHOWING TLE error.

include<stdio.h>

int main() { int t,a,b,i,j,k,ct; scanf("%d",&t); for(i=1;i<=t;i++) { scanf("%d%d",&a,&b);

  for(j=a;j<=b;j++)
   { for(k=2,ct=0;k<=j/2;k++)
    {if(j%k==0)
       ct++;}


      if(ct==0)
       printf("%d",j);

}

printf("\n"); } return 0; }

asked 06 Sep '17, 09:24

robof1's gravatar image

2★robof1
1
accept rate: 0%

Can you share the question link please

(06 Sep '17, 09:26) kunnu1202★

Please share the question link. From the code it looks like you are trying to find all the primes between a and b. If so, your approach will time out. Just give a large a and b and run your code on your system. You can see how long it takes.

Then study segmented sieve and implement that. If you don't know sieve, start with sieve-of-eratosthenes and do a couple of problems from that. Then move to segmented sieve.

link

answered 06 Sep '17, 10:43

dragonemperor's gravatar image

3★dragonemperor
89321134
accept rate: 10%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,718
×1,134
×250

question asked: 06 Sep '17, 09:24

question was seen: 238 times

last updated: 06 Sep '17, 10:43