First, 1 is not a prime.
Secondly, the millionth number is 15,485,863, so you need to be prepared for some data processing.
Thirdly, you probably want to use the Sieve of Eratosthenes; here is a simple version:
function sieve(n) bits := makeArray(0..n, True) for p from 2 to n step 1 if bits[p] output p for i from p*p to n step p bits[i] := False
This may not work for the size of the array, which you will need to calculate the first millions of primes. In this case, you will want to implement a segmented sieve of eratosthenes.
I did a great job with simple numbers on my blog, including essay , which provides an optimized Eratosthenes sieve, with implementations in five programming languages.
No matter what you do, with any programming language, you should be able to calculate the first millions of primes in no more than a few seconds.
source share