You might want the good ol' Sieve
en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Or the spankin' new one
en.wikipedia.org/wiki/Sieve_of_Atkin
Which is by the way already written out nicely for you
cr.yp.to/primegen.html
primegen is a small, fast library to generate prime numbers in order. It generates the 50847534 primes up to 1000000000 in just 8 seconds on a Pentium II-350; it prints them in decimal in just 35 seconds.