-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsieve.h
56 lines (49 loc) · 1.56 KB
/
sieve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed under the MIT License.
#include "boolean_set.h"
#include "list.h"
#include "object.h"
#include "primality_test.h"
/** Represents the sequence of primes in a superset of [2, `max`). */
struct Sieve
{
struct List primes;
struct BooleanSet composites;
long long max;
};
/** Represents the sequence of primes in a superset of [2, `max`). */
typedef struct Sieve* Sieve;
/**
* Initializes a `Sieve` instance.
*
* @param instance the `Sieve` instance.
* @param max the exclusive minimum upper bound of the prime sequence.
* @return The exception; otherwise, `0`.
*/
Exception sieve(Sieve instance, long long max);
/**
* Returns the `n`-th prime. The `n`-th prime is approximately `n log n`.
* For convenience, `n` is a zero-based index.
*
* @param instance the `Sieve` instance.
* @param n the zero-based term number in the prime sequence.
* @return The `n`-th prime.
*/
long long sieve_prime(Sieve instance, size_t n);
/**
* Determines if a number is a prime.
*
* @param instance the `Sieve` instance.
* @param n the number to test.
* @param fallback `NULL` or a fallback algorithm for determining if a number is
* a prime. The `fallback` argument may be used if `n` is
* outside the interval [2, `max`).
* @return The supposed primality of `n`.
*/
Primality sieve_test(Sieve instance, long long n, PrimalityTest fallback);
/**
* Frees all resources.
*
* @param instance the `Sieve` instance. This method corrupts the `instance`
* argument.
*/
void finalize_sieve(Sieve instance);