|
14 | 14 | #include <vector>
|
15 | 15 | #include <iterator>
|
16 | 16 | #include <iostream>
|
| 17 | +#include <cmath> |
| 18 | +#include <thread> |
17 | 19 |
|
18 |
| -namespace boost { namespace math |
| 20 | +namespace boost { namespace math { namespace detail |
19 | 21 | {
|
20 |
| - |
21 | 22 | // https://mathworld.wolfram.com/SieveofEratosthenes.html
|
22 | 23 | // https://www.cs.utexas.edu/users/misra/scannedPdf.dir/linearSieve.pdf
|
23 |
| -template<class Z, class OutputIterator> |
24 |
| -auto prime_sieve(Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) |
| 24 | +template<class Z, class Container> |
| 25 | +void linear_sieve(Z upper_bound, Container &c) |
25 | 26 | {
|
26 |
| - static_assert(std::is_integral<Z>::value, "No primes for floating point types"); |
27 |
| - BOOST_ASSERT_MSG(upper_bound + 1 < std::numeric_limits<Z>::max(), "Type Overflow"); |
28 |
| - std::vector<Z> least_divisors(upper_bound + 1, 0); |
29 |
| - std::deque<Z> primes; |
| 27 | + Z least_divisors_size{upper_bound + 1}; |
| 28 | + Z *least_divisors{new Z[least_divisors_size]{0}}; |
30 | 29 |
|
31 | 30 | for (Z i{2}; i <= upper_bound; ++i)
|
32 | 31 | {
|
33 | 32 | if (least_divisors[i] == 0)
|
34 | 33 | {
|
35 | 34 | least_divisors[i] = i;
|
36 |
| - primes.emplace_back(i); |
| 35 | + c.emplace_back(i); |
37 | 36 | }
|
38 | 37 |
|
39 |
| - for (size_t j{}; j < least_divisors.size(); ++j) |
| 38 | + for (size_t j{}; j < least_divisors_size; ++j) |
40 | 39 | {
|
41 |
| - if (j >= primes.size()) |
| 40 | + if (j >= c.size()) |
42 | 41 | {
|
43 | 42 | break;
|
44 | 43 | }
|
45 | 44 |
|
46 |
| - else if (primes[j] > least_divisors[i]) |
| 45 | + else if (c[j] > least_divisors[i]) |
47 | 46 | {
|
48 | 47 | break;
|
49 | 48 | }
|
50 | 49 |
|
51 |
| - else if (i * primes[j] > upper_bound) |
| 50 | + else if (i * c[j] > upper_bound) |
52 | 51 | {
|
53 | 52 | break;
|
54 | 53 | }
|
55 | 54 |
|
56 | 55 | else
|
57 | 56 | {
|
58 |
| - least_divisors[i * primes[j]] = primes[j]; |
| 57 | + least_divisors[i * c[j]] = c[j]; |
59 | 58 | }
|
60 | 59 | }
|
61 | 60 | }
|
62 | 61 |
|
63 |
| - auto it{primes.begin()}; |
64 |
| - while (*it < lower_bound && it != primes.end()) |
| 62 | + delete[] least_divisors; |
| 63 | +} |
| 64 | + |
| 65 | +template<class Z, class Container> |
| 66 | +void prime_table(Z upper_bound, Container &c) |
| 67 | +{ |
| 68 | + Z i{2}; |
| 69 | + unsigned counter{}; |
| 70 | + |
| 71 | + while (i <= upper_bound && counter < 9999) // 10k elements are in the lookup table |
65 | 72 | {
|
66 |
| - primes.pop_front(); |
67 |
| - ++it; |
| 73 | + c.emplace_back(i); |
| 74 | + ++counter; |
| 75 | + i = static_cast<Z>(boost::math::prime(counter)); |
68 | 76 | }
|
69 |
| - |
70 |
| - return std::move(primes.begin(), primes.end(), output); |
71 | 77 | }
|
72 | 78 |
|
73 |
| -template<class Z, class OutputIterator> |
74 |
| -auto prime_range(Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) |
| 79 | +template<class Z, class Container> |
| 80 | +void mask_sieve(Z lower_bound, Z upper_bound, Container &c) |
75 | 81 | {
|
76 |
| - if (upper_bound <= 104729) |
| 82 | + Z limit{static_cast<Z>(std::floor(std::sqrt(upper_bound))) + 1}; |
| 83 | + std::vector<Z> primes; |
| 84 | + primes.reserve(limit / std::log(limit)); |
| 85 | + |
| 86 | + boost::math::detail::linear_sieve(limit, primes); |
| 87 | + |
| 88 | + const Z n{upper_bound - lower_bound + 1}; |
| 89 | + bool *mask{new bool[n + 1]{false}}; |
| 90 | + |
| 91 | + for (size_t i{}; i < primes.size(); ++i) |
77 | 92 | {
|
78 |
| - Z i{2}; |
79 |
| - unsigned counter {}; |
80 |
| - std::deque<Z> primes; |
81 |
| - while (i <= upper_bound) |
| 93 | + Z lower_limit = std::floor(lower_bound / primes[i]) * primes[i]; |
| 94 | + |
| 95 | + if (lower_limit < lower_bound) |
| 96 | + { |
| 97 | + lower_limit += primes[i]; |
| 98 | + } |
| 99 | + |
| 100 | + if (lower_limit == primes[i]) |
| 101 | + { |
| 102 | + lower_limit += primes[i]; |
| 103 | + } |
| 104 | + |
| 105 | + for (Z j{lower_limit}; j <= upper_bound; j += primes[i]) |
| 106 | + { |
| 107 | + mask[j - lower_bound] = true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Numbers which are not masked in range, are prime |
| 112 | + for (Z i{lower_bound}; i <= upper_bound; i++) |
| 113 | + { |
| 114 | + if (!mask[i - lower_bound]) |
82 | 115 | {
|
83 | 116 | if (i >= lower_bound)
|
84 | 117 | {
|
85 |
| - primes.emplace_back(i); |
| 118 | + c.emplace_back(i); |
86 | 119 | }
|
87 |
| - |
88 |
| - ++counter; |
89 |
| - i = static_cast<Z>(boost::math::prime(counter)); |
90 | 120 | }
|
| 121 | + } |
| 122 | + |
| 123 | + delete[] mask; |
| 124 | +} |
| 125 | +} // End namespace detail |
| 126 | + |
| 127 | +template<typename Z, class OutputIterator> |
| 128 | +auto prime_sieve(Z upper_bound, OutputIterator output) -> decltype(output) |
| 129 | +{ |
| 130 | + static_assert(std::is_integral<Z>::value, "No primes for floating point types"); |
| 131 | + BOOST_ASSERT_MSG(upper_bound + 1 < std::numeric_limits<Z>::max(), "Type Overflow"); |
91 | 132 |
|
92 |
| - return std::move(primes.begin(), primes.end(), output); |
| 133 | + std::vector<Z> primes; |
| 134 | + primes.reserve(upper_bound / std::log(upper_bound)); |
| 135 | + |
| 136 | + if (upper_bound <= 104729) |
| 137 | + { |
| 138 | + boost::math::detail::prime_table(upper_bound, primes); |
93 | 139 | }
|
94 | 140 |
|
95 | 141 | else
|
96 | 142 | {
|
97 |
| - return prime_sieve(lower_bound, upper_bound, output); |
| 143 | + std::vector<Z> small_primes; |
| 144 | + small_primes.reserve(1000); |
| 145 | + |
| 146 | + // Spilt into two vectors and merge after joined to avoid data races |
| 147 | + std::thread t1([upper_bound, &small_primes]{boost::math::detail::prime_table(static_cast<Z>(104729), small_primes);}); |
| 148 | + std::thread t2([upper_bound, &primes]{boost::math::detail::mask_sieve(static_cast<Z>(104729), upper_bound, primes);}); |
| 149 | + |
| 150 | + t1.join(); |
| 151 | + t2.join(); |
| 152 | + primes.insert(primes.begin(), small_primes.begin(), small_primes.end()); |
| 153 | + } |
| 154 | + |
| 155 | + return std::move(primes.begin(), primes.end(), output); |
| 156 | +} |
| 157 | + |
| 158 | +template<class Z, class OutputIterator> |
| 159 | +auto prime_range(Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) |
| 160 | +{ |
| 161 | + std::vector<Z> primes; |
| 162 | + primes.reserve(upper_bound / std::log(upper_bound)); |
| 163 | + |
| 164 | + boost::math::prime_sieve(upper_bound, std::back_inserter(primes)); |
| 165 | + |
| 166 | + auto it{primes.begin()}; |
| 167 | + while(*it < lower_bound && it != primes.end()) |
| 168 | + { |
| 169 | + ++it; |
98 | 170 | }
|
| 171 | + |
| 172 | + return std::move(it, primes.end(), output); |
99 | 173 | }
|
100 | 174 |
|
101 | 175 | template<class Z, class OutputIterator>
|
|
0 commit comments