You can try Eduard's approach.. it will get your program much faster..
But the strange thing is my approach is the same as yours, and I got AC in 3.045..
Thanks a ton for that reply. Anyways, can you tell me why that happened. Have have quite a few accepted codes where I have declared big arays in main itself.
Did this happen because I declared too many big arrays in main.
Abhiram
Here no need of modification in sieve function. U can store the number of factors of each number in an array. Then print sum of the factors of each number upto n. After precalculation the complexity is O(1).
I saw many replies about improving the speed. My Java program precalculated all the answers, but still took 2.6sec at first. After using StringBuilder as a buffer to store the output instead of outputting right away, it took about 0.6sec. That means most of the time goes into the output routine and not the real calcuations. Looks like the test set is extremely large.