1 #include <symengine/prime_sieve.h>
8 #ifdef HAVE_SYMENGINE_PRIMESIEVE
9 #include <primesieve.hpp>
21 bool Sieve::_clear =
true;
22 unsigned Sieve::_sieve_size = 32 * 1024 * 8;
24 void Sieve::set_clear(
bool clear)
35 void Sieve::set_sieve_size(
unsigned size)
37 #ifdef HAVE_SYMENGINE_PRIMESIEVE
38 primesieve::set_sieve_size(size);
40 _sieve_size = size * 1024 * 8;
44 void Sieve::_extend(
unsigned limit)
47 #ifdef HAVE_SYMENGINE_PRIMESIEVE
48 if (_primes.
back() < limit)
49 primesieve::generate_primes(_primes.
back() + 1, limit, &_primes);
51 const unsigned sqrt_limit
53 unsigned start = _primes.
back() + 1;
56 if (sqrt_limit >= start) {
58 start = _primes.
back() + 1;
61 unsigned segment = _sieve_size;
63 for (; start <= limit; start += 2 * segment) {
64 unsigned finish =
std::min(start + segment * 2 + 1, limit);
65 is_prime[std::slice(0, segment, 1)] =
true;
68 for (
unsigned index = 1; index < _primes.
size()
69 and _primes[index] * _primes[index] <= finish;
71 unsigned n = _primes[index];
72 unsigned multiple = (start / n + 1) * n;
73 if (multiple % 2 == 0)
75 if (multiple > finish)
77 std::slice sl = std::slice((multiple - start) / 2,
78 1 + (finish - multiple) / (2 * n), n);
83 for (
unsigned n = start + 1; n <= finish; n += 2) {
84 if (is_prime[(n - start) / 2])
104 Sieve::iterator::iterator(
unsigned max)
110 Sieve::iterator::iterator()
116 Sieve::iterator::~iterator()
122 unsigned Sieve::iterator::next_prime()
125 if (_index >= _primes.
size()) {
126 unsigned extend_to = _primes[_index - 1] * 2;
127 if (_limit > 0 and _limit < extend_to) {
131 if (_index >= _primes.
size()) {
135 return _primes[_index++];
T back_inserter(T... args)
static void generate_primes(std::vector< unsigned > &primes, unsigned limit)
Main namespace for SymEngine package.
RCP< const Basic > max(const vec_basic &arg)
Canonicalize Max: