prev_prime.cpp

Iterate backwards over primes using primesieve::iterator.

#include <primesieve.hpp>
#include <cstdlib>
#include <iostream>
int main(int argc, char** argv)
{
uint64_t limit = 10000000000ull;
if (argc > 1)
limit = std::atol(argv[1]);
primesieve::iterator it;
it.skipto(limit);
uint64_t prime = it.prev_prime();
uint64_t sum = 0;
// iterate over the primes below 10^9
for (; prime > 0; prime = it.prev_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::skipto
void skipto(uint64_t start, uint64_t stop_hint=get_max_stop())
Reset the primesieve iterator to start.
primesieve::iterator::prev_prime
uint64_t prev_prime()
Get the previous prime.
Definition: iterator.hpp:79
primesieve.hpp
primesieve C++ API.