I added

[code] tags to the above posts to improve readability. Please use these in the future.

**[Ravisekhar]: It is enough to loop thru until j < (int) Math.sqrt(i) .** Good idea, but slightly off. If i is 9 for example, Math.sqrt(9) is 3. But the loop will only

test j = 2, not j = 3, because of the requirement that j < 3. This needs slight modification. I'd also point out that the sqrt() method need only be called once for each i, before the j loop begins. It's needlessly slow to evaluate sqrt() many times inside the j loop since it's the same for each i.

Another thing to think about - do you really need to test devision by

*all* the numbers from 2 to sqrt(i)? For example, do you need to divide by 4? If the number were divisible by 4, it also would have been divisible by 2, which was tested earlier. Similarly 6 could be skipped, and 8, 9, 10, 12... In fact you could skip every non-prime number here. Is there some way to do that? (Hint: in the original problem, you're building a list of prime numbers. Can we use that?)