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

×

PRIME1 getting TLE!!!

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

sandeep9's gravatar image

3★sandeep9
4782827
accept rate: 4%


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

link

answered 21 May '15, 18:50

pratku123's gravatar image

4★pratku123
1.8k4933
accept rate: 14%

edited 21 May '15, 19:43

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★

read about segmented sieve then give it a try using it.

link

answered 23 May '15, 11:23

pulkitsja's gravatar image

4★pulkitsja
162
accept rate: 20%

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.

link

answered 23 May '15, 11:28

pulkitsja's gravatar image

4★pulkitsja
162
accept rate: 20%

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:

×303
×69

question asked: 21 May '15, 15:59

question was seen: 1,396 times

last updated: 23 May '15, 11:28