primesieve 12.4
Loading...
Searching...
No Matches
Macros | Functions
primesieve.hpp File Reference

primesieve C++ API. More...

#include <primesieve/iterator.hpp>
#include <primesieve/primesieve_error.hpp>
#include <primesieve/StorePrimes.hpp>
#include <stdint.h>
#include <string>
Include dependency graph for primesieve.hpp:

Macros

#define PRIMESIEVE_VERSION   "12.4"
 
#define PRIMESIEVE_VERSION_MAJOR   12
 
#define PRIMESIEVE_VERSION_MINOR   4
 

Functions

template<typename vect >
void primesieve::generate_primes (uint64_t stop, vect *primes)
 Appends the primes <= stop to the end of the primes vector.
 
template<typename vect >
void primesieve::generate_primes (uint64_t start, uint64_t stop, vect *primes)
 Appends the primes inside [start, stop] to the end of the primes vector.
 
template<typename vect >
void primesieve::generate_n_primes (uint64_t n, vect *primes)
 Appends the first n primes to the end of the primes vector.
 
template<typename vect >
void primesieve::generate_n_primes (uint64_t n, uint64_t start, vect *primes)
 Appends the first n primes >= start to the end of the primes vector.
 
uint64_t primesieve::nth_prime (int64_t n, uint64_t start=0)
 Find the nth prime.
 
uint64_t primesieve::count_primes (uint64_t start, uint64_t stop)
 Count the primes within the interval [start, stop].
 
uint64_t primesieve::count_twins (uint64_t start, uint64_t stop)
 Count the twin primes within the interval [start, stop].
 
uint64_t primesieve::count_triplets (uint64_t start, uint64_t stop)
 Count the prime triplets within the interval [start, stop].
 
uint64_t primesieve::count_quadruplets (uint64_t start, uint64_t stop)
 Count the prime quadruplets within the interval [start, stop].
 
uint64_t primesieve::count_quintuplets (uint64_t start, uint64_t stop)
 Count the prime quintuplets within the interval [start, stop].
 
uint64_t primesieve::count_sextuplets (uint64_t start, uint64_t stop)
 Count the prime sextuplets within the interval [start, stop].
 
void primesieve::print_primes (uint64_t start, uint64_t stop)
 Print the primes within the interval [start, stop] to the standard output.
 
void primesieve::print_twins (uint64_t start, uint64_t stop)
 Print the twin primes within the interval [start, stop] to the standard output.
 
void primesieve::print_triplets (uint64_t start, uint64_t stop)
 Print the prime triplets within the interval [start, stop] to the standard output.
 
void primesieve::print_quadruplets (uint64_t start, uint64_t stop)
 Print the prime quadruplets within the interval [start, stop] to the standard output.
 
void primesieve::print_quintuplets (uint64_t start, uint64_t stop)
 Print the prime quintuplets within the interval [start, stop] to the standard output.
 
void primesieve::print_sextuplets (uint64_t start, uint64_t stop)
 Print the prime sextuplets within the interval [start, stop] to the standard output.
 
uint64_t primesieve::get_max_stop ()
 Returns the largest valid stop number for primesieve.
 
int primesieve::get_sieve_size ()
 Get the current set sieve size in KiB.
 
int primesieve::get_num_threads ()
 Get the current set number of threads.
 
void primesieve::set_sieve_size (int sieve_size)
 Set the sieve size in KiB (kibibyte).
 
void primesieve::set_num_threads (int num_threads)
 Set the number of threads for use in primesieve::count_*() and primesieve::nth_prime().
 
std::string primesieve::primesieve_version ()
 Get the primesieve version number, in the form “i.j”.
 

Detailed Description

primesieve C++ API.

primesieve is a library for fast prime number generation, in case an error occurs a primesieve::primesieve_error exception (derived form std::runtime_error) is thrown.

Copyright (C) 2024 Kim Walisch, kim.w.nosp@m.alis.nosp@m.ch@gm.nosp@m.ail..nosp@m.com

This file is distributed under the BSD License.

Function Documentation

◆ count_primes()

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

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

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

Note that each call to count_primes() incurs an initialization overhead of O(sqrt(stop)) even if the interval [start, stop] is tiny. Hence if you have written an algorithm that makes many calls to count_primes() it may be preferable to use a primesieve::iterator which needs to be initialized only once.

Examples
count_primes.cpp.

◆ count_quadruplets()

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

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

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

◆ count_quintuplets()

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

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

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

◆ count_sextuplets()

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

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

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

◆ count_triplets()

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

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

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

◆ count_twins()

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

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

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

◆ generate_n_primes() [1/2]

template<typename vect >
void primesieve::generate_n_primes ( uint64_t  n,
uint64_t  start,
vect *  primes 
)
inline

Appends the first n primes >= start to the end of the primes vector.

@vect: std::vector or other vector type that is API compatible with std::vector.

◆ generate_n_primes() [2/2]

template<typename vect >
void primesieve::generate_n_primes ( uint64_t  n,
vect *  primes 
)
inline

Appends the first n primes to the end of the primes vector.

@vect: std::vector or other vector type that is API compatible with std::vector.

Examples
primes_vector.cpp.

◆ generate_primes() [1/2]

template<typename vect >
void primesieve::generate_primes ( uint64_t  start,
uint64_t  stop,
vect *  primes 
)
inline

Appends the primes inside [start, stop] to the end of the primes vector.

@vect: std::vector or other vector type that is API compatible with std::vector.

◆ generate_primes() [2/2]

template<typename vect >
void primesieve::generate_primes ( uint64_t  stop,
vect *  primes 
)
inline

Appends the primes <= stop to the end of the primes vector.

@vect: std::vector or other vector type that is API compatible with std::vector.

Examples
primes_vector.cpp.

◆ get_max_stop()

uint64_t primesieve::get_max_stop ( )

Returns the largest valid stop number for primesieve.

Returns
2^64-1 (UINT64_MAX).

◆ nth_prime()

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

Find the nth prime.

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

Note that each call to nth_prime(n, start) incurs an initialization overhead of O(sqrt(start)) even if n is tiny. Hence it is not a good idea to use nth_prime() repeatedly in a loop to get the next (or previous) prime. For this use case it is better to use a primesieve::iterator which needs to be initialized only once.

Parameters
nif n = 0 finds the 1st prime >= start,
if n > 0 finds the nth prime > start,
if n < 0 finds the nth prime < start (backwards).
Examples
nth_prime.cpp.

◆ set_num_threads()

void primesieve::set_num_threads ( int  num_threads)

Set the number of threads for use in primesieve::count_*() and primesieve::nth_prime().

By default all CPU cores are used.

◆ set_sieve_size()

void primesieve::set_sieve_size ( int  sieve_size)

Set the sieve size in KiB (kibibyte).

The best sieving performance is achieved with a sieve size of your CPU's L1 or L2 cache size (per core).

Precondition
sieve_size >= 16 && <= 8192.