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

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*!=-1)

    {

    temp[k++]=a*;

    }

    }




    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


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.

Thanks for the suggestion pratku123, i tried both optimisations,still its getting TLE!!!
Link to my updated solution
http://www.codechef.com/viewsolution/6995848