About
=====

This directory (src/primesieve) contains primesieve's sieve of
Eratosthenes implementation. Most files that start with a capital
letter contain a class of the same name. The header files of these
classes are stored in include/primesive. Below is a short explanation
of the most important classes and files.

C++ classes
-----------
* PrimeSieve
  PrimeSieve objects provide an easy API for prime sieving,
  PrimeSieve::sieve(start, stop) sieves the primes within the
  interval [start, stop]. The classes below are all used directly or
  indirectly for sieving.

* ParallelPrimeSieve (derived from PrimeSieve)
  ParallelPrimeSieve launches multiple threads using std::async and
  each thread sieves a part of the range [start, stop] using a
  PrimeSieve object. At the end all partial results are combined to
  get the final result.

* SieveOfEratosthenes (abstract class)
  Implementation of the segmented sieve of Eratosthenes using a bit
  array with 30 numbers per byte, each byte of the sieve array holds
  the 8 offsets k = { 7, 11, 13, 17, 19, 23, 29, 31 }.
  Its main methods are addSievingPrime(uint64_t) which is called
  consecutively for all primes <= sqrt(n) and sieve() which sieves
  the primes in [start, stop]. SieveOfEratosthenes uses the
  EratSmall, EratMedium and EratBig classes to cross-off multiples.

* PrimeGenerator (derived from SieveOfEratosthenes)
  After a segment has been sieved (using SieveOfEratosthenes)
  PrimeGenerator is used to reconstruct primes and prime k-tuplets
  from 1 bits of the sieve array.

* SievingPrimes (derived from SieveOfEratosthenes)
  Generates the sieving primes <= sqrt(stop) for PrimeGenerator.

* PreSieve
  Pre-sieve multiples of small primes e.g. <= 19 to speed up the
  sieve of Eratosthenes. Upon creation the multiples of small primes
  are removed from a buffer. Later this buffer is simply copied to the
  sieve array to remove (pre-sieve) the multiples of small primes.

* Wheel (abstract class)
  Wheel factorization is used to skip multiples of small primes e.g.
  <= 7 to speed up the sieve of Eratosthenes. The abstract Wheel
  class is used to initialize sieving primes i.e. addSievingPrime()
  calculates the first multiple >= start of each sieving prime and
  the position within the SieveOfEratosthenes array of that multiple
  (multipleIndex). The unsetBit() method is used to cross-off a
  multiple (unset a bit) and to calculate the sieving prime's next
  multiple.

* EratSmall (derived from Wheel)
  Segmented sieve of Eratosthenes algorithm with a hard-coded
  modulo 30 wheel that skips multiples of 2, 3 and 5. This algorithm
  is optimized for small sieving primes that have many multiples in
  each segment. EratSmall is a further optimized implementation of
  Achim Flammenkamp's algorithm.[9]

* EratMedium (derived from Wheel)
  Segmented sieve of Eratosthenes algorithm with a fixed modulo 210
  wheel that skips multiples of 2, 3, 5 and 7. The wheel is
  implemented using a precomputed lookup table (wheel210 array from
  Wheel.cpp). This algorithm is optimized for medium sieving primes
  with a few multiples per segment.

* EratBig (derived from Wheel)
  Segmented sieve of Eratosthenes algorithm with Tomás Oliveira's
  improvement for big sieving primes [11] and a fixed modulo 210
  wheel that skips multiples of 2, 3, 5 and 7. The wheel is
  implemented using a precomputed lookup table (wheel210 array from
  Wheel.cpp). EratBig is optimized for big sieving primes that have
  less than one multiple per segment.

* iterator
  primesieve::iterator allows to easily iterate over primes. It
  provides next_prime() and prev_prime() methods. primesieve::iterator
  works using a cache of primes of size sqrt(stop), once there are
  no more primes available in the cache primesieve::iterator will
  reload the cache with new primes.

Other files
-----------
* include/primesieve.hpp
  This header contains primesieve's new API first released in
  primesieve-5.0. It is mostly a wrapper around the methods of the
  PrimeSieve and ParallelPrimeSieve classes.

* api.cpp
  primesieve C++ API.
  This file contains the implementations of the functions declared
  in include/primesieve.hpp.

* api-c.cpp
  primesieve C API.
  This file contains the implementations of the functions declared
  in include/primesieve.h.

* include/primesieve/config.hpp
  Contains compile time constants that set the size of various
  arrays and limits within primesieve. You can set these constants
  according to your CPU type to get the best performance.

References
==========

[1] Richard C. Singleton, "An efficient prime number generator",
    Communications of the ACM 12, 563-564, 1969.
[2] R. P. Brent, "The first occurrence of large gaps between
    successive primes", Mathematics of Computation, 27:959-963,
    1973.
[3] C. Bays and R. Hudson, "The segmented sieve of Eratosthenes and
    primes in arithmetic progressions to 10^12", BIT 17:121 127,
    1977.
[4] Paul Pritchard, "Fast compact prime number sieves (among
    others)", Journal of Algorithms 4 (1983), 332-344.
[5] J. Sorenson, "An Introduction To Prime Number Sieves", Computer
    Science Technical Report Vol. 909, January 1990.
    http://research.cs.wisc.edu/techreports/1990/TR909.pdf
[6] J. Sorenson, "An analysis of two prime number sieves", Computer
    Science Technical Report Vol. 1028, June 1991.
    http://research.cs.wisc.edu/techreports/1991/TR1028.pdf
[7] J. Sorenson and I. Parberry, "Two Fast Parallel Prime Number
    Sieves", Information and Computation, Vol. 114, No. 1, 1994.
    http://larc.unt.edu/ian/pubs/sieve.pdf
[8] J. Sorenson, "Trading Time for Space in Prime Number Sieves",
    Lecture Notes in Computer Science, Vol. 1423 (1998), 179-194.
[9] Achim Flammenkamp, "The Art of Prime Sieving", 1998.
    http://wwwhomes.uni-bielefeld.de/achim/prime_sieve.html
[10] Jörg Richstein, "Segmentierung und Optimierung von Algorithmen
    zu Problemen aus der Zahlentheorie", Gießen, Univ., Diss., 1999.
[11] Tomás Oliveira e Silva, "Fast implementation of the segmented
    sieve of Eratosthenes", 2002.
    http://www.ieeta.pt/~tos/software/prime_sieve.html
[12] A. Járai and E. Vatai, "Cache optimized linear sieve", Acta
    Univ. Sapientiae, Informatica, 3, 2 (2011) 205-223.
    http://www.acta.sapientia.ro/acta-info/C3-2/info32-5.pdf
