## About

primesieve is a free software program and C/C++ library that generates primes using a highly optimized sieve of Eratosthenes implementation. It counts the primes below 10^10 in just 0.57 seconds on an Intel Core i7-4770 CPU (4 x 3.4GHz) from 2013. primesieve can generate primes and prime k-tuplets up to 2^64.

## Performance

primesieve is the fastest open source prime number generator! It generates primes at least 50 times faster (single-threaded) than an ordinary C/C++ sieve of Eratosthenes implementation and more than 10,000 times faster than trial‑division. Ben Buhrow's yafu contains another highly optimized sieve of Eratosthenes implementation but primesieve runs a little faster on most CPUs. primesieve also outperforms primegen the fastest sieve of Atkin implementation on the web by more than 30 percent. Here is a historical list of fast prime sieve implementations.

## Timings

x | Prime Count |
AMD Phenom II X4 945 (4 x 3.0GHz, 64K L1 Data Cache) |
Intel Core i7-4770 (4 x 3.4GHz, 32K L1 Data Cache) |
IBM POWER8 (22 x 3.0GHz, 64K L1 Data Cache) |

10^{7} |
664,579 | 0.00s | 0.00s | 0.00s |

10^{8} |
5,761,455 | 0.01s | 0.01s | 0.01s |

10^{9} |
50,847,534 | 0.06s | 0.05s | 0.07s |

2^{32} |
203,280,221 | 0.54s | 0.26s | 0.15s |

10^{10} |
455,052,511 | 0.66s | 0.57s | 0.24s |

10^{11} |
4,118,054,813 | 8.57s | 6.63s | 1.72s |

10^{12} |
37,607,912,018 | 122.02s | 77.79s | 19.34s |

10^{13} |
346,065,536,839 | 1664.68s | 926.83 | 225.92s |

The above benchmarks were run on a 64-bit Linux operating
system and primesieve was compiled using
GCC 4.8. For each benchmark
primesieve used all available CPU cores (AMD: 4 threads, Intel: 8 threads,
IBM: 66 threads) and the sieve size was set to the CPU's L1 data cache
size per core.

## CPU scaling

primesieve is multi-threaded and scales linearly up to a very large number of CPU cores if the interval is sufficiently large. The above benchmark was run on a system with 8 physical CPU cores and 23 GB of memory. At each start offset the primes inside an interval of size 10^11 were counted using different numbers of threads.

## Algorithms

primesieve generates primes using the segmented sieve of Eratosthenes with wheel factorization, this algorithm has a complexity of operations and uses space.

Segmentation is currently the best known practical improvement to the sieve of Eratosthenes. Instead of sieving the interval [2, n] at once one subdivides the sieve interval into a number of equal sized segments that are then sieved consecutively. Segmentation drops the memory requirement of the sieve of Eratosthenes from to . The segment size is usually chosen to fit into the CPU's fast L1 or L2 cache memory which significantly speeds up sieving. A segmented version of the sieve of Eratosthenes was first published by Singleton in 1969 [1]. Here is a simple implementation of the segmented sieve of Eratosthenes.

Wheel factorization is used to skip multiples of small primes. If a
*k*th wheel is added to the sieve of Eratosthenes then only those
multiples are crossed off that are coprime to the first *k*
primes, i.e. multiples that are divisible by any of the first *k*
primes are skipped. The 1st wheel considers only odd numbers, the 2nd
wheel (modulo 6) skips multiples of 2 and 3, the 3rd wheel (modulo 30)
skips multiples of 2, 3, 5 and so on. Pritchard has shown in [2] that
the running time of the sieve of Eratosthenes can be reduced by a factor of
if the wheel size is
but for cache reasons the sieve of Eratosthenes usually performs best
with a modulo 30 or 210 wheel. Sorenson explains wheels in [3].

Additionally primesieve uses Tomás Oliveira e Silva's cache-friendly bucket list algorithm if needed [4]. This algorithm is relatively new, it has been devised by Tomás Oliveira e Silva in 2001 in order to speed up the segmented sieve of Eratosthenes for prime numbers past 32 bits. The idea is to store the sieving primes into lists of buckets with each list being associated with a segment. A list of sieving primes related to a specific segment contains only those primes that have multiple occurrence(s) in that segment. Whilst sieving a segment only the primes of the related list are used for sieving and each prime is reassigned to the list responsible for its next multiple when processed. The benefit of this approach is that it is now possible to use segments (i.e. sieve arrays) smaller than without deteriorating efficiency, this is important as only small segments that fit into the CPU's L1 or L2 cache provide fast memory access.

## Implementation

primesieve is written entirely in C++ and does not depend on external libraries. It's speed is mainly due to the segmentation of the sieve of Eratosthenes which prevents cache misses when crossing off multiples in the sieve array and the use of a bit array instead of a boolean sieve array. primesieve reuses and improves ideas from other great sieve of Eratosthenes implementations, namely Achim Flammenkamp's prime_sieve.c, Tomás Oliveira e Silva's A1 implementation and the author's older ecprime all written in the late '90s and '00s. Furthermore primesieve contains new optimizations to increase instruction-level parallelism and more efficiently uses the larger number of registers in today's CPUs.

Optimizations used in primesieve

- Uses a bit array with 8 flags each 30 numbers for sieving
- Pre-sieves multiples of small primes ≤ 19
- Compresses the sieving primes in order to improve cache efficiency [5]
- Starts crossing off multiples at the square
- Uses a modolo 210 wheel that skips multiples of 2, 3, 5 and 7
- Uses specialized algorithms for small, medium and big sieving primes
- Uses a custom memory pool (for big sieving primes)
- Processes two sieving primes per loop iteration to increase instruction-level parallelism
- Parallelized (multi-threaded) using OpenMP

Highly optimized inner loop

primesieve's inner sieving loop has been optimized using
extreme loop unrolling,
on average crossing off a multiple uses just 1.375 instructions on
x86-64 CPUs. Below is the assembly GCC generates for primesieve's inner sieving
loop, each `andb`

instruction unsets a bit (crosses off a multiple)
in the sieve array.

```
; primesieve inner sieving loop
; Uses only 11 instructions (x86-64) to cross-off the next 8 multiples
.L99:
andb $-3, (%rax)
andb $-9, (%rax,%r14)
andb $127, (%rax,%r12)
andb $-33, (%rax,%rbp)
andb $-2, (%rax,%r10)
andb $-65, (%rax,%rbx)
andb $-5, (%rax,%r9)
andb $-17, (%rax,%r11)
addq %r13, %rax
cmpq %rdi, %rax
jb .L99
```

This README file contains more technical implementation details.

## C/C++ library

Below is an example that shows how to generate primes in C++ using libprimesieve. All of primesieve's functions are also exposed as C API. You can browse primesieve's API online.

```
#include <primesieve.hpp>
#include <iostream>
#include <vector>
int main()
{
// store the primes below 1000
std::vector<int> primes;
primesieve::generate_primes(1000, &primes);
primesieve::iterator pi;
uint64_t prime;
// iterate over the primes below 10^9
while ((prime = pi.next_prime()) < 1000000000)
std::cout << prime << std::endl;
return 0;
}
```

The Build From Source page explains how to build libprimesieve and how to link against it.

## Bindings for other languages

primesieve supports C++ and C directly, and has bindings available for a few other programming languages:

- Python: primesieve-python
- Ruby: primesieve-ruby

Many thanks to the developers of these bindings!

## References and notes

- R. C. Singleton, "An efficient prime number generator", Communications of the ACM 12, 563-564, 1969.
- Paul Pritchard, "Fast compact prime number sieves (among others)", Journal of Algorithms 4 (1983), 332-344.
- Jonathan Sorenson, "An analysis of two prime number sieves", Computer Science Technical Report Vol. 1028, 1991.
- Tomás Oliveira e Silva, "Fast implementation of the segmented sieve of Eratosthenes", 2002.
- Actually not the sieving primes are compressed but their sieve and wheel indexes.