×

# PRIME1 getting TLE!!!

 0 I dont know why my solution is geting TLE!!!Please have a look http://www.codechef.com/viewsolution/6943810 I am implementing sieve of erasothenes to caclulate primes upto sqrt(10^9) and then applying these primes in the given segment to print all prime no's..then why it's giving TLE?? where is my code getting slow and how to correct that..Please help asked 21 May '15, 15:59 3★sandeep9 478●2●8●27 accept rate: 4%

 0 There are a few problems: 1. You are using the sqrt() function inside the for loop. This is executing the sqrt() function every time the condition of the for loop is checked. Store this in some variable before the starting of the loop. 2. You can further remove the following loop in sieve function: for(i=2;i<=up;i++) { if(a[i]!=-1) { temp[k++]=a[i]; } } Instead perform this in the main sieve loop. 3. I have solved this problem using sieve. The link to solution: http://www.codechef.com/viewsolution/6989950 The link to my previous solution without sieve: http://www.codechef.com/viewsolution/2683274 answered 21 May '15, 18:50 1.8k●4●9●33 accept rate: 14% Thanks for the suggestion pratku123, i tried both optimisations,still its getting TLE!!! Link to my updated solution http://www.codechef.com/viewsolution/6995848 (23 May '15, 09:03) sandeep93★
 0 read about segmented sieve then give it a try using it. answered 23 May '15, 11:23 16●2 accept rate: 20%
 0 http://www.spoj.com/problems/PRINT/ this is a rather challenging version of the same problem you can try this as well.this would require segmented sieve. answered 23 May '15, 11:28 16●2 accept rate: 20%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×303
×69

question asked: 21 May '15, 15:59

question was seen: 1,396 times

last updated: 23 May '15, 11:28