40 uintmax_t prime : (
sizeof(uintmax_t) * 4);
42 uintmax_t _pad1 : (
sizeof(uintmax_t) * 4);
45 size_t sieve_len : (
sizeof(size_t) * 8 - 1);
74 if (ps->
sieve != NULL) {
81 free_prime_sieve(pc->
ps);
99 if (!prime_cache_size) {
100 prime_cache = (uintmax_t *) malloc(
sizeof(uintmax_t) * 4);
104 prime_cache[3] = prime_cache_max = 7;
105 prime_cache_size = 4;
108 if (pc->
idx < prime_cache_idx) {
109 uintmax_t p = prime_cache[pc->
idx++];
115 if (pc->
ps == NULL) {
123 while(
next_p(pc->
ps) < prime_cache[pc->
idx - 1]) {}
126 if (pc->
idx == prime_cache_idx) {
128 prime_cache_size == prime_cache_idx
129 #ifdef PRIME_CACHE_SIZE_LIMIT
130 && prime_cache_size < PRIME_CACHE_SIZE_LIMIT
133 size_t new_size = prime_cache_size * 2;
134 #ifdef PRIME_CACHE_SIZE_LIMIT 135 if (new_size > PRIME_CACHE_SIZE_LIMIT) {
136 new_size = PRIME_CACHE_SIZE_LIMIT;
139 uintmax_t *tmp = (uintmax_t *) realloc(prime_cache, new_size *
sizeof(uintmax_t));
142 prime_cache_size = new_size;
143 prime_cache[prime_cache_idx++] = prime_cache_max = p;
147 prime_cache[prime_cache_idx++] = p;
199 bool candidate_in_sieve =
false;
200 size_t candidate_index = -1;
201 for (
size_t i = 0; i < ps->
sieve_len * 2; i += 2) {
203 step = ps->
sieve[i + 1];
204 candidate_in_sieve =
true;
209 if (!candidate_in_sieve) {
216 step = ps->
prime * 2;
224 candidate_in_sieve =
false;
225 for (
size_t i = 0; i < ps->
sieve_len * 2; i += 2) {
226 if (ps->
sieve[i] == candidate) {
227 candidate_in_sieve =
true;
231 }
while (candidate_in_sieve);
232 if (candidate_index != -1) {
233 ps->
sieve[candidate_index] = candidate;
316 advance_prime_factor_counter,
338 uintmax_t ret =
next(iter);
prime_counter prime_counter0()
The simplest constructor for the prime number generator.
Definition: primes.h:178
static uintmax_t prime_cache_max
Definition: primes.h:53
uintmax_t advance_prime_factor_counter(prime_factor_counter *pfc)
The function to advance a prime factor iterator.
Definition: primes.h:294
#define IteratorInitHead(advance,...)
The base macro for all iterator initialization functions in this project.
Definition: iterator.h:111
#define IteratorTail(return_type, struct_type)
The base definition macro for all iterators in this project.
Definition: iterator.h:46
size_t sieve_len
The length of the sieve state (divided by 2)
Definition: primes.h:45
static size_t prime_cache_idx
Definition: primes.h:55
size_t idx
The current position of the counter.
Definition: primes.h:27
uintmax_t * sieve
The sieve state used to generate new primes stored as pairs of [value, step].
Definition: primes.h:46
static size_t prime_cache_size
Definition: primes.h:54
def prime_factors
Definition: p0003.py:98
bool exhausted
An indicator that the iterator has stopped.
Definition: iterator.h:231
#define ExtendInit(name, value)
The extension macro for initializing more complicated Iterators.
Definition: iterator.h:168
prime_counter source
The source of new reference prime numbers.
Definition: primes.h:38
#define free_iterator(it)
The generic destructor for iterators.
Definition: iterator.h:207
void free_prime_counter(prime_counter *pc)
The destructor for the prime number counter.
Definition: primes.h:79
uintmax_t is_composite(uintmax_t n)
Tells you if a number is composite, and if so, its smallest prime factor.
Definition: primes.h:333
#define AddDestructor(func)
The extension macro for initializing Iterators with a destructor.
Definition: iterator.h:145
A cached prime number generator.
Definition: primes.h:26
#define next(state)
The macro to advance generic iterators.
Definition: iterator.h:180
prime_counter pc
The prime number generator being used to test.
Definition: primes.h:271
uintmax_t stop
The point where the counter is exhausted.
Definition: primes.h:28
void free_prime_factor_counter(prime_factor_counter *pfc)
Definition: primes.h:282
prime_sieve * ps
A (currently unused) source for new prime numbers.
Definition: primes.h:29
uintmax_t prime_squared
The reference prime squared.
Definition: primes.h:39
uintmax_t candidate
The current candidate prime number.
Definition: primes.h:47
prime_sieve prime_sieve0()
#define next_p(state)
The macro to advance generic iterator pointers.
Definition: iterator.h:192
uintmax_t current
The prime number most recently tested.
Definition: primes.h:270
#define IterationHead(it)
The base macro for all iteration functions in this project.
Definition: iterator.h:74
bool is_prime(uintmax_t n)
Tells you if a number is prime.
Definition: primes.h:354
void free_prime_sieve(prime_sieve *ps)
The destructor for the prime number sieve.
Definition: primes.h:72
prime_factor_counter prime_factors(uintmax_t n)
The base constructor for the prime factors iterator.
Definition: primes.h:314
prime_sieve prime_sieve0()
The constructor for the prime number sieve.
Definition: primes.h:248
uintmax_t prime
The current reference prime.
Definition: primes.h:40
uintmax_t advance_prime_counter(prime_counter *pc)
The function to advance a prime number generator.
Definition: primes.h:94
uintmax_t target
The current target for prime factorization.
Definition: primes.h:269
#define unlikely(x)
Indicates to the compiler (if supported) that this branch is unlikely to occur and it should arrange ...
Definition: math.h:152
An implementation of Python-like iterators and generators using macros to maintain static typing...
prime_counter prime_counter1(uintmax_t stop)
The base constructor for the prime number generator.
Definition: primes.h:164
uintmax_t advance_prime_sieve(prime_sieve *ps)
The function to advance a prime sieve iterator.
Definition: primes.h:190
static uintmax_t * prime_cache
Definition: primes.h:51