primesieve  6.1
Public Member Functions | List of all members
primesieve::iterator Class Reference

primesieve::iterator allows to easily iterate over primes both forwards and backwards. More...

#include <iterator.hpp>

Public Member Functions

 iterator (uint64_t start=0, uint64_t stop_hint=get_max_stop())
 Create a new iterator object. More...
 
void skipto (uint64_t start, uint64_t stop_hint=get_max_stop())
 Reinitialize this iterator object to start. More...
 
uint64_t next_prime ()
 Advance the iterator by one position. More...
 
uint64_t prev_prime ()
 Get the previous prime, or 0 if input <= 2 e.g. More...
 

Detailed Description

primesieve::iterator allows to easily iterate over primes both forwards and backwards.

Generating the first prime has a complexity of O(r log log r) operations with r = n^0.5, after that any additional prime is generated in amortized O(log n log log n) operations. The memory usage is about pi(n^0.5) * 16 bytes.

Examples:
prev_prime.cpp, and primesieve_iterator.cpp.

Constructor & Destructor Documentation

◆ iterator()

primesieve::iterator::iterator ( uint64_t  start = 0,
uint64_t  stop_hint = get_max_stop() 
)

Create a new iterator object.

Parameters
startGenerate primes > start (or < start).
stop_hintStop number optimization hint, gives significant speed up if few primes are generated. E.g. if you want to generate the primes below 1000 use stop_hint = 1000.

Member Function Documentation

◆ next_prime()

uint64_t primesieve::iterator::next_prime ( )
inline

Advance the iterator by one position.

Returns
The next prime.
Examples:
primesieve_iterator.cpp.

◆ prev_prime()

uint64_t primesieve::iterator::prev_prime ( )
inline

Get the previous prime, or 0 if input <= 2 e.g.

prev_prime(2) = 0.

Examples:
prev_prime.cpp.

◆ skipto()

void primesieve::iterator::skipto ( uint64_t  start,
uint64_t  stop_hint = get_max_stop() 
)

Reinitialize this iterator object to start.

Parameters
startGenerate primes > start (or < start).
stop_hintStop number optimization hint, gives significant speed up if few primes are generated. E.g. if you want to generate the primes below 1000 use stop_hint = 1000.
Examples:
prev_prime.cpp.

The documentation for this class was generated from the following file: