primesieve_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 primesieve_iterator.h:
This graph shows which files directly or indirectly include this file:

## Classes

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

## Functions

void | primesieve_init (primesieve_iterator *pi)
| Initialize the primesieve iterator before first using it. More...
void | primesieve_free_iterator (primesieve_iterator *pi)
| Free all memory. More...
void | primesieve_skipto (primesieve_iterator *pi, uint64_t start, uint64_t stop_hint)
| Set the primesieve iterator to start. More...
static uint64_t | primesieve_next_prime (primesieve_iterator *pi)
| Get the next prime. More...
static uint64_t | primesieve_previous_prime (primesieve_iterator *pi)
| 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. primesieve_iterator objects are very convenient to use at the cost of being slightly slower than the primesieve_callback_primes() functions.

The primesieve_iterator.c example shows how to use primesieve_iterator. If any error occurs errno is set to EDOM and primesieve_next_prime() and primesieve_previous_prime() return PRIMESIEVE_ERROR.

Copyright (C) 2015 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. See the COPYING file in the top level directory.

## Function Documentation

void primesieve_free_iterator | ( | primesieve_iterator * | pi | ) |

Free all memory.

Examples:
  previous_prime.c, and primesieve_iterator.c.

void primesieve_init | ( | primesieve_iterator * | pi | ) |

Initialize the primesieve iterator before first using it.

Examples:
  previous_prime.c, and primesieve_iterator.c.

static uint64_t primesieve_next_prime | ( | primesieve_iterator * | pi | ) |

inline static

Get the next prime.

Examples:
  primesieve_iterator.c.

static uint64_t primesieve_previous_prime | ( | primesieve_iterator * | pi | ) |

inline static

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

previous_prime(2) = 0.

Examples:
  previous_prime.c.

void primesieve_skipto | ( | primesieve_iterator * | pi,
|  | uint64_t | start,
|  | uint64_t | stop_hint
| ) |  |

Set the primesieve iterator to start.

Parameters

  start | Generate primes > start (or < start).
  stop_hint | Stop 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().

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

Examples:
  previous_prime.c.