Disclaimer: This is not an officially supported Google product.
This program uses the Sieve of Eratosthenes to generate primes up to a given bound.
On a standard low-end Intel Core i5 it produces ~7.9M primes per second.
It uses approximately 0.024√n bytes of RAM to compute primes up to n.
The program emits primes to stdout encoded as 64-bit binary little-endian numbers.
This format is easily readable by programs, and can be even mmap
-ed as an
int64_t[]
array. Also allows to quickly read an N-th prime.
$ clang++ -O3 sieve.cc -o sieve
g++
works just as well, it just produces slightly slower (~15%) binary.
The first 50.000.000 primes should satisfy the following hash:
$ ./sieve 982451653 | sha256sum
17d28fa909939b450dbd6b8923a1001c61bdc8cedb5e44a07f9f90b4d36ab279 -