primesieve_iterator.cpp

Iterate over primes using primesieve::iterator.

#include <primesieve.hpp>
#include <cstdlib>
#include <iostream>
int main(int argc, char** argv)
{
primesieve::iterator it;
uint64_t limit = 10000000000ull;
uint64_t prime = it.next_prime();
uint64_t sum = 0;
if (argc > 1)
limit = std::atol(argv[1]);
// iterate over the primes below 10^9
for (; prime < limit; prime = it.next_prime())
sum += prime;
std::cout << "Sum of primes < " << limit << " = " << sum << std::endl;
// Note that since sum is a 64-bit variable the result
// will be incorrect (due to integer overflow) if
// limit > 10^10. However we do allow limits > 10^10
// since this is useful for benchmarking.
if (limit > 10000000000ull)
std::cerr << "Warning: sum is likely incorrect due to 64-bit integer overflow!" << std::endl;
return 0;
}
primesieve::iterator
primesieve::iterator allows to easily iterate over primes both forwards and backwards.
Definition: iterator.hpp:33
primesieve::iterator::next_prime
uint64_t next_prime()
Get the next prime.
Definition: iterator.hpp:66
primesieve.hpp
primesieve C++ API.