SPOJ - TDPRIMES - TLE with Prime Sieves

Problem: https://www.spoj.com/problems/TDPRIMES/
Code: https://ide.codingblocks.com/s/79508

My code is taking 3+ second, the time limit is about 1 second.
Any resource would be helpful.

Thanks!