primesieve.h File Reference

primesieve C API. primesieve is a library for fast prime number generation. In case an error occurs errno is set to EDOM and PRIMESIEVE_ERROR is returned. More...

`#include <primesieve/iterator.h>`
`#include <stdint.h>`
`#include <stddef.h>`
Include dependency graph for primesieve.h:

## Macros

#define | PRIMESIEVE_VERSION "7.5"
#define | PRIMESIEVE_VERSION_MAJOR 7
#define | PRIMESIEVE_VERSION_MINOR 5
#define | PRIMESIEVE_ERROR ((uint64_t) ~((uint64_t) 0))
| primesieve functions return PRIMESIEVE_ERROR (UINT64_MAX) if any error occurs.

## Enumerations

enum | { SHORT_PRIMES, USHORT_PRIMES, INT_PRIMES, UINT_PRIMES, LONG_PRIMES, ULONG_PRIMES, LONGLONG_PRIMES, ULONGLONG_PRIMES, INT16_PRIMES, UINT16_PRIMES, INT32_PRIMES, UINT32_PRIMES, INT64_PRIMES, UINT64_PRIMES }

## Functions

void * | primesieve_generate_primes (uint64_t start, uint64_t stop, size_t *size, int type)
| Get an array with the primes inside the interval [start, stop]. More...
void * | primesieve_generate_n_primes (uint64_t n, uint64_t start, int type)
| Get an array with the first n primes >= start. More...
uint64_t | primesieve_nth_prime (int64_t n, uint64_t start)
| Find the nth prime. More...
uint64_t | primesieve_count_primes (uint64_t start, uint64_t stop)
| Count the primes within the interval [start, stop]. More...
uint64_t | primesieve_count_twins (uint64_t start, uint64_t stop)
| Count the twin primes within the interval [start, stop]. More...
uint64_t | primesieve_count_triplets (uint64_t start, uint64_t stop)
| Count the prime triplets within the interval [start, stop]. More...
uint64_t | primesieve_count_quadruplets (uint64_t start, uint64_t stop)
| Count the prime quadruplets within the interval [start, stop]. More...
uint64_t | primesieve_count_quintuplets (uint64_t start, uint64_t stop)
| Count the prime quintuplets within the interval [start, stop]. More...
uint64_t | primesieve_count_sextuplets (uint64_t start, uint64_t stop)
| Count the prime sextuplets within the interval [start, stop]. More...
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. More...
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). More...
void | primesieve_set_num_threads (int num_threads)
| Set the number of threads for use in primesieve_count_*() and primesieve_nth_prime(). More...
void | primesieve_free (void *primes)
| Deallocate a primes array created using the primesieve_generate_primes() or primesieve_generate_n_primes() functions.
const char * | 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 errno is set to EDOM and PRIMESIEVE_ERROR is returned.

Copyright (C) 2019 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.

## Enumeration Type Documentation

## anonymous enum

anonymous enum

Enumerator
SHORT_PRIMES | Generate primes of short type.
USHORT_PRIMES | Generate primes of unsigned short type.
INT_PRIMES | Generate primes of int type.
UINT_PRIMES | Generate primes of unsigned int type.
LONG_PRIMES | Generate primes of long type.
ULONG_PRIMES | Generate primes of unsigned long type.
LONGLONG_PRIMES | Generate primes of long long type.
ULONGLONG_PRIMES | Generate primes of unsigned long long type.
INT16_PRIMES | Generate primes of int16_t type.
UINT16_PRIMES | Generate primes of uint16_t type.
INT32_PRIMES | Generate primes of int32_t type.
UINT32_PRIMES | Generate primes of uint32_t type.
INT64_PRIMES | Generate primes of int64_t type.
UINT64_PRIMES | Generate primes of uint64_t type.

## Function Documentation

## primesieve_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 primesieve_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 primesieve_count_primes() it may be preferable to use a primesieve::iterator which needs to be initialized only once.

Examples
  count_primes.c.

## primesieve_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.

## primesieve_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.

## primesieve_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.

## primesieve_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.

## primesieve_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.

## primesieve_generate_n_primes()

void* primesieve_generate_n_primes | ( | uint64_t | n,
|  | uint64_t | start,
|  | int | type
| ) |  |

Get an array with the first n primes >= start.

Parameters

  type | The type of the primes to generate, e.g. INT_PRIMES.

Examples
  store_primes_in_array.c.

## primesieve_generate_primes()

void* primesieve_generate_primes | ( | uint64_t | start,
|  | uint64_t | stop,
|  | size_t * | size,
|  | int | type
| ) |  |

Get an array with the primes inside the interval [start, stop].

Parameters

  size | The size of the returned primes array.
  type | The type of the primes to generate, e.g. INT_PRIMES.

Examples
  store_primes_in_array.c.

## primesieve_get_max_stop()

uint64_t primesieve_get_max_stop | ( |  | ) |

Returns the largest valid stop number for primesieve.

Returns
  2^64-1 (UINT64_MAX).

## primesieve_nth_prime()

uint64_t primesieve_nth_prime | ( | int64_t | n,
|  | uint64_t | start
| ) |  |

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 primesieve_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 primesieve_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

  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).

Examples
  nth_prime.c.

## primesieve_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.

## primesieve_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 >= 8 && <= 4096.