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
There are a few problems:
- 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.
- You can further remove the following loop in sieve function:
Instead perform this in the main sieve loop.
3. I have solved this problem using sieve. The link to solution:
The link to my previous solution without sieve:
Thanks for the suggestion pratku123, i tried both optimisations,still its getting TLE!!!
Link to my updated solution
read about segmented sieve then give it a try using it.
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.