PRIME1 getting TLE!!!

I dont know why my solution is geting TLE!!!Please have a look CodeChef: Practical coding for everyone

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:

  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:








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:

read about segmented sieve then give it a try using it. - Problem PRINT this is a rather challenging version of the same problem you can try this as well.this would require segmented sieve.

Thanks for the suggestion pratku123, i tried both optimisations,still its getting TLE!!!
Link to my updated solution