primesieve Namespace Reference

All of primesieve's C++ functions and classes are declared inside this namespace. More...

## Classes

class | Callback
| callback interface class. More...
class | iterator
| primesieve::iterator allows to easily iterate over primes both forwards and backwards. More...
class | primesieve_error
| primesieve throws a primesieve_error exception if an error occurs that cannot be handled e.g. More...

## Enumerations

enum | { MAX_THREADS = -1 }

## Functions

template<typename T >
void | generate_primes (uint64_t stop, std::vector< T > *primes)
| Store the primes <= stop in the primes vector. More...
template<typename T >
void | generate_primes (uint64_t start, uint64_t stop, std::vector< T > *primes)
| Store the primes within the interval [start, stop] in the primes vector. More...
template<typename T >
void | generate_n_primes (uint64_t n, std::vector< T > *primes)
| Store the first n primes in the primes vector.
template<typename T >
void | generate_n_primes (uint64_t n, uint64_t start, std::vector< T > *primes)
| Store the first n primes >= start in the primes vector. More...
uint64_t | nth_prime (int64_t n, uint64_t start=0)
| Find the nth prime. More...
uint64_t | parallel_nth_prime (int64_t n, uint64_t start=0)
| Find the nth prime in parallel. More...
uint64_t | count_primes (uint64_t start, uint64_t stop)
| Count the primes within the interval [start, stop]. More...
uint64_t | count_twins (uint64_t start, uint64_t stop)
| Count the twin primes within the interval [start, stop]. More...
uint64_t | count_triplets (uint64_t start, uint64_t stop)
| Count the prime triplets within the interval [start, stop]. More...
uint64_t | count_quadruplets (uint64_t start, uint64_t stop)
| Count the prime quadruplets within the interval [start, stop]. More...
uint64_t | count_quintuplets (uint64_t start, uint64_t stop)
| Count the prime quintuplets within the interval [start, stop]. More...
uint64_t | count_sextuplets (uint64_t start, uint64_t stop)
| Count the prime sextuplets within the interval [start, stop]. More...
uint64_t | parallel_count_primes (uint64_t start, uint64_t stop)
| Count the primes within the interval [start, stop] in parallel. More...
uint64_t | parallel_count_twins (uint64_t start, uint64_t stop)
| Count the twin primes within the interval [start, stop] in parallel. More...
uint64_t | parallel_count_triplets (uint64_t start, uint64_t stop)
| Count the prime triplets within the interval [start, stop] in parallel. More...
uint64_t | parallel_count_quadruplets (uint64_t start, uint64_t stop)
| Count the prime quadruplets within the interval [start, stop] in parallel. More...
uint64_t | parallel_count_quintuplets (uint64_t start, uint64_t stop)
| Count the prime quintuplets within the interval [start, stop] in parallel. More...
uint64_t | parallel_count_sextuplets (uint64_t start, uint64_t stop)
| Count the prime sextuplets within the interval [start, stop] in parallel. More...
void | print_primes (uint64_t start, uint64_t stop)
| Print the primes within the interval [start, stop] to the standard output. More...
void | print_twins (uint64_t start, uint64_t stop)
| Print the twin primes within the interval [start, stop] to the standard output. More...
void | print_triplets (uint64_t start, uint64_t stop)
| Print the prime triplets within the interval [start, stop] to the standard output. More...
void | print_quadruplets (uint64_t start, uint64_t stop)
| Print the prime quadruplets within the interval [start, stop] to the standard output. More...
void | print_quintuplets (uint64_t start, uint64_t stop)
| Print the prime quintuplets within the interval [start, stop] to the standard output. More...
void | print_sextuplets (uint64_t start, uint64_t stop)
| Print the prime sextuplets within the interval [start, stop] to the standard output. More...
void | callback_primes (uint64_t start, uint64_t stop, void(*callback)(uint64_t prime))
| Call back the primes within the interval [start, stop]. More...
void | callback_primes (uint64_t start, uint64_t stop, primesieve::Callback< uint64_t > *callback)
| Call back the primes within the interval [start, stop]. More...
int | get_sieve_size ()
| Get the current set sieve size in kilobytes.
int | get_num_threads ()
| Get the current set number of threads. More...
uint64_t | get_max_stop ()
| Returns the largest valid stop number for primesieve. More...
void | set_sieve_size (int sieve_size)
| Set the sieve size in kilobytes. More...
void | set_num_threads (int num_threads)
| Set the number of threads for use in subsequent primesieve::parallel_* function calls. More...
bool | primesieve_test ()
| Run extensive correctness tests. More...
std::string | primesieve_version ()
| Get the primesieve version number, in the form “i.j.k”.

## Detailed Description

All of primesieve's C++ functions and classes are declared inside this namespace.

## Enumeration Type Documentation

anonymous enum

Enumerator
MAX_THREADS | Use all CPU cores for prime sieving.

## Function Documentation

void primesieve::callback_primes | ( | uint64_t | start,
|  | uint64_t | stop,
|  | void(*)(uint64_t prime) | callback
| ) |  |

Call back the primes within the interval [start, stop].

Parameters

  callback | A callback function.

Precondition
  stop <= 2^64 - 2^32 * 10.

Examples:
  callback_primes.cpp.

void primesieve::callback_primes | ( | uint64_t | start,
|  | uint64_t | stop,
|  | primesieve::Callback< uint64_t > * | callback
| ) |  |

Call back the primes within the interval [start, stop].

Parameters

  callback | An object derived from primesieve::Callback<uint64_t>.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::count_primes | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the primes within the interval [start, stop].

Precondition
  stop <= 2^64 - 2^32 * 10.

Examples:
  count_primes.cpp.

uint64_t primesieve::count_quadruplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime quadruplets within the interval [start, stop].

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::count_quintuplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime quintuplets within the interval [start, stop].

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::count_sextuplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime sextuplets within the interval [start, stop].

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::count_triplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime triplets within the interval [start, stop].

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::count_twins | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the twin primes within the interval [start, stop].

Precondition
  stop <= 2^64 - 2^32 * 10.

template<typename T >

void primesieve::generate_n_primes | ( | uint64_t | n,
|  | uint64_t | start,
|  | std::vector< T > * | primes
| ) |  |

inline

Store the first n primes >= start in the primes vector.

Precondition
  start <= 2^64 - 2^32 * 10.

template<typename T >

void primesieve::generate_primes | ( | uint64_t | stop,
|  | std::vector< T > * | primes
| ) |  |

inline

Store the primes <= stop in the primes vector.

Precondition
  stop <= 2^64 - 2^32 * 10.

Examples:
  store_primes_in_vector.cpp.

template<typename T >

void primesieve::generate_primes | ( | uint64_t | start,
|  | uint64_t | stop,
|  | std::vector< T > * | primes
| ) |  |

inline

Store the primes within the interval [start, stop] in the primes vector.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::get_max_stop | ( |  | ) |

Returns the largest valid stop number for primesieve.

Returns
  (2^64-1) - (2^32-1) * 10.

int primesieve::get_num_threads | ( |  | ) |

Get the current set number of threads.

Note
  By default MAX_THREADS (-1) is returned.

uint64_t primesieve::nth_prime | ( | int64_t | n,
|  | uint64_t | start = `0`
| ) |  |

Find the nth prime.

Parameters

  n | if n = 0 finds the 1st prime >= start, if n > 0 finds the nth prime > start, if n < 0 finds the nth prime < start (backwards).

Precondition
  start <= 2^64 - 2^32 * 11.

Examples:
  nth_prime.cpp.

uint64_t primesieve::parallel_count_primes | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the primes within the interval [start, stop] in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Precondition
  stop <= 2^64 - 2^32 * 10.

Examples:
  count_primes.cpp.

uint64_t primesieve::parallel_count_quadruplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime quadruplets within the interval [start, stop] in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::parallel_count_quintuplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime quintuplets within the interval [start, stop] in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::parallel_count_sextuplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime sextuplets within the interval [start, stop] in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::parallel_count_triplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the prime triplets within the interval [start, stop] in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::parallel_count_twins | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Count the twin primes within the interval [start, stop] in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Precondition
  stop <= 2^64 - 2^32 * 10.

uint64_t primesieve::parallel_nth_prime | ( | int64_t | n,
|  | uint64_t | start = `0`
| ) |  |

Find the nth prime in parallel.

By default all CPU cores are used, use primesieve::set_num_threads(int) to change the number of threads.

Parameters

  n | if n = 0 finds the 1st prime >= start, if n > 0 finds the nth prime > start, if n < 0 finds the nth prime < start (backwards).

Precondition
  start <= 2^64 - 2^32 * 11.

bool primesieve::primesieve_test | ( |  | ) |

Run extensive correctness tests.

The tests last about one minute on a quad core CPU from 2013 and use up to 1 gigabyte of memory.

Returns
  true if success else false.

void primesieve::print_primes | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Print the primes within the interval [start, stop] to the standard output.

Precondition
  stop <= 2^64 - 2^32 * 10.

void primesieve::print_quadruplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Print the prime quadruplets within the interval [start, stop] to the standard output.

Precondition
  stop <= 2^64 - 2^32 * 10.

void primesieve::print_quintuplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Print the prime quintuplets within the interval [start, stop] to the standard output.

Precondition
  stop <= 2^64 - 2^32 * 10.

void primesieve::print_sextuplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Print the prime sextuplets within the interval [start, stop] to the standard output.

Precondition
  stop <= 2^64 - 2^32 * 10.

void primesieve::print_triplets | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Print the prime triplets within the interval [start, stop] to the standard output.

Precondition
  stop <= 2^64 - 2^32 * 10.

void primesieve::print_twins | ( | uint64_t | start,
|  | uint64_t | stop
| ) |  |

Print the twin primes within the interval [start, stop] to the standard output.

Precondition
  stop <= 2^64 - 2^32 * 10.

void primesieve::set_num_threads | ( | int | num_threads | ) |

Set the number of threads for use in subsequent primesieve::parallel_* function calls.

Note that this only changes the number of threads for the current process.

Parameters

  num_threads | Number of threads for sieving or MAX_THREADS to use all CPU cores.

void primesieve::set_sieve_size | ( | int | sieve_size | ) |

Set the sieve size in kilobytes.

The best sieving performance is achieved with a sieve size of your CPU's L1 data cache size (per core). For sieving >= 10^17 a sieve size of your CPU's L2 cache size sometimes performs better.

Parameters

  sieve_size | Sieve size in kilobytes.

Precondition
  sieve_size >= 1 && sieve_size <= 2048.