primesieve  7.7
Classes | Functions
iterator.h File Reference

primesieve_iterator allows to easily iterate over primes both forwards and backwards. More...

#include <stdint.h>
#include <stddef.h>
Include dependency graph for iterator.h:
This graph shows which files directly or indirectly include this file:


struct  primesieve_iterator
 C prime iterator, please refer to iterator.h for more information. More...


void primesieve_init (primesieve_iterator *it)
 Initialize the primesieve iterator before first using it.
void primesieve_free_iterator (primesieve_iterator *it)
 Free all memory.
void primesieve_skipto (primesieve_iterator *it, uint64_t start, uint64_t stop_hint)
 Reset the primesieve iterator to start. More...
static uint64_t primesieve_next_prime (primesieve_iterator *it)
 Get the next prime. More...
static uint64_t primesieve_prev_prime (primesieve_iterator *it)
 Get the previous prime. 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 PrimePi(n^0.5) * 8 bytes.

The primesieve_iterator.c example shows how to use primesieve_iterator. If any error occurs primesieve_next_prime() and primesieve_prev_prime() return PRIMESIEVE_ERROR. Furthermore primesieve_iterator.is_error is initialized to 0 and set to 1 if any error occurs.

Copyright (C) 2019 Kim Walisch, kim.w[email protected]alis[email protected][email protected][email protected]ail.[email protected]com

This file is distributed under the BSD License. See the COPYING file in the top level directory.

Function Documentation

◆ primesieve_next_prime()

static uint64_t primesieve_next_prime ( primesieve_iterator it)

Get the next prime.

Returns UINT64_MAX if next prime > 2^64.


◆ primesieve_prev_prime()

static uint64_t primesieve_prev_prime ( primesieve_iterator it)

Get the previous prime.

primesieve_prev_prime(n) returns 0 for n <= 2. Note that primesieve_next_prime() runs up to 2x faster than primesieve_prev_prime(). Hence if the same algorithm can be written using either primesieve_prev_prime() or primesieve_next_prime() it is preferable to use primesieve_next_prime().


◆ primesieve_skipto()

void primesieve_skipto ( primesieve_iterator it,
uint64_t  start,
uint64_t  stop_hint 

Reset the primesieve iterator to start.

startGenerate primes > start (or < start).
stop_hintStop number optimization hint. E.g. if you want to generate the primes below 1000 use stop_hint = 1000, if you don't know use primesieve_get_max_stop().
prev_prime.c, and primesieve_iterator.c.