You might also want to check out the contest forum, we had a prime number generating contest awhile back with some good algorithms.

As for your program, you need to reinitialize i before the while...