Your company here — click to reach over 10,000 unique daily visitors

primesieve - Man Page

generate prime numbers


primesieve [START] STOP [OPTION]...


Generate the prime numbers and/or prime k-tuplets inside [START, STOP] (< 2^64) using the segmented sieve of Eratosthenes. primesieve includes a number of extensions to the sieve of Eratosthenes which significantly improve performance: multiples of small primes are pre-sieved, it uses wheel factorization to skip multiples with small prime factors and it uses the bucket sieve algorithm which improves cache efficiency when sieving > 2^32. primesieve is also multi-threaded, it uses all available CPU cores by default for counting primes and for finding the nth prime.

The segmented sieve of Eratosthenes has a runtime complexity of O(n log log n) operations and it uses O(n^(1/2)) bits of memory. More specifically primesieve uses 8 bytes per sieving prime, hence its memory usage can be approximated by PrimePi(n^(1/2)) * 8 bytes (per thread).


-c[NUM+], --count[=NUM+]

Count primes and/or prime k-tuplets, 1 <= NUM <= 6. Count primes: -c or --count, count twin primes: -c2 or --count=2, count prime triplets: -c3 or --count=3, ... You can also count primes and prime k-tuplets at the same time, e.g. -c123 counts primes, twin primes and prime triplets.


Print CPU information: CPU name, frequency, number of cores, cache sizes, ...

-d,  --dist=DIST

Sieve the interval [START, START + DIST].

-h,  --help

Print this help menu.

-n,  --nth-prime

Find the nth prime, e.g. 100 -n finds the 100th prime. If 2 numbers N START are provided finds the nth prime > START, e.g. 2 100 -n finds the 2nd prime > 100.


Turn off the progressing status.

-p[NUM], --print[=NUM]

Print primes or prime k-tuplets, 1 <= NUM <= 6. Print primes: -p, print twin primes: -p2, print prime triplets: -p3, ...

-q,  --quiet

Quiet mode, prints less output.

-R,  --RiemannR

Approximate PrimePi(x) using the Riemann R function: R(x).


Approximate the nth prime using the inverse Riemann R function: R^-1(x).

-s,  --size=SIZE

Set the size of the sieve array in KiB, 16 <= SIZE <= 8192. By default primesieve uses a sieve size that matches your CPU’s L1 cache size (per core) or is slightly smaller than your CPU’s L2 cache size. This setting is crucial for performance, on exotic CPUs primesieve sometimes fails to determine the CPU’s cache sizes which usually causes a big slowdown. In this case you can get a significant speedup by manually setting the sieve size to your CPU’s L1 or L2 cache size (per core).

-S,  --stress-test[=MODE]

Run a stress test. The MODE can be either CPU (default) or RAM. The CPU MODE uses little memory (< 5 MiB per thread) and puts the highest load on the CPU. The RAM MODE on the other hand uses much more memory than the CPU MODE (each thread uses about 1.16 GiB), but the CPU usually won’t get as hot as in the CPU MODE. Stress testing keeps on running until either a miscalculation occurs (due to a hardware issue) or the timeout expires. The default timeout is 24 hours, the timeout can be changed using the --timeout=SECS option. By default the stress test uses a number of threads that matches the number of CPU cores, the number of threads can be changed using the --threads=NUM option.


Run various correctness tests (< 1 minute).

-t,  --threads=NUM

Set the number of threads, 1 <= NUM <= CPU cores. By default primesieve uses all available CPU cores for counting primes and for finding the nth prime.


Print the time elapsed in seconds.


Set the stress test timeout in seconds. Units of time for seconds, minutes, hours, days and years are also supported with the suffix s, m, h, d or y. E.g. --timeout 10m sets a timeout of 10 minutes. The default stress test timeout is 24 hours.

-v,  --version

Print version and license information.


primesieve 1000

Count the primes <= 1000.

primesieve 1e6 --print

Print the primes <= 10^6.

primesieve 1e6 --print > primes.txt

Store the primes <= 10^6 in a text file.

primesieve 2^32 --print=2

Print the twin primes <= 2^32.

primesieve 1e16 --dist=1e10 --threads=1

Count the primes inside [10^16, 10^16 + 10^10] using a single thread.




Kim Walisch <kim.walisch@gmail.com>