Loading...
Searching...
No Matches
primesieve_iterator.c

Iterate over primes using C primesieve_iterator.

Iterate over primes using C primesieve_iterator.

#include <primesieve.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
uint64_t limit = 10000000000ull;
if (argc > 1)
limit = atol(argv[1]);
primesieve_iterator it;
primesieve_init(&it);
/* Indicate exact bounds to improve performance */
primesieve_jump_to(&it, 0, limit);
uint64_t sum = 0;
uint64_t prime = 0;
/* Iterate over the primes <= 10^9 */
while ((prime = primesieve_next_prime(&it)) <= limit)
sum += prime;
printf("Sum of the primes <= %" PRIu64 ": %" PRIu64 "\n", limit, sum);
primesieve_free_iterator(&it);
return 0;
}
primesieve_jump_to
void primesieve_jump_to(primesieve_iterator *it, uint64_t start, uint64_t stop_hint)
Reset the primesieve iterator to start.
primesieve_next_prime
static uint64_t primesieve_next_prime(primesieve_iterator *it)
Get the next prime.
Definition iterator.h:152
primesieve_free_iterator
void primesieve_free_iterator(primesieve_iterator *it)
Free all memory.
primesieve_init
void primesieve_init(primesieve_iterator *it)
Initialize the primesieve iterator before first using it.
primesieve.h
primesieve C API.
primesieve_iterator
C prime iterator, please refer to iterator.h for more information.
Definition iterator.h:52