-
Notifications
You must be signed in to change notification settings - Fork 248
Prime Number Functions #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
4015fbc
3ee737b
9512bb6
d762398
5375a1d
a684dbd
3e4db8a
dd8a61c
4debd0d
2a7e031
a68910e
386cc4d
d79eddb
3fdf917
6ca245b
7d3a520
a1ac504
35d2aa1
243a299
2eadeff
173ce0d
23fba36
f7b45fd
d687d5e
e51d727
1245d27
2052081
9f81e0d
55ea045
31a2105
f545928
d780db5
2639bed
94fc1ac
6ebb906
0521854
b233a80
f95c2cf
8e2e29a
1a24f16
c63f1f1
b7d4256
fbc38c8
2e46b81
6759ede
1b40403
97244be
fa04133
c4a89c8
91836f6
6d6b19f
81e4a6c
0b8f1d5
5ba0a1d
8e240f7
7c0cbbf
3d9b77c
302fb5f
9792a23
84a69f0
5dc3523
0dbe69c
eee2c86
f5d789a
f2277e3
1d2f03c
0d9d31b
c361cde
66c2642
de1f331
eaea5f9
e8196f3
cb5d978
830ccc4
4dc3eb2
90be100
a772782
2d1461f
e7cdb32
29eef88
b5a28b5
6c26b53
980bfe7
07e6f58
86b9e5a
f19149e
1ad0d51
5f6d06a
670b06d
7c7a491
e507ba5
c9cb41c
e8b71a0
d649f65
ed98892
c3b3934
e9aa05d
7c5d792
2bbfad2
ddc5c8a
f517c00
a356b47
2212e45
b12e2dc
f39a4d8
eafbefc
cb36b89
16c2354
0b1a690
8c883d1
f357e0f
a4f2d89
d16e562
91be2c3
d9ae8c2
7d22010
b541987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…d libraries. Fixed -Wextra errors. [CI SKIP]
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,11 @@ | |
| #include <vector> | ||
| #include <iterator> | ||
| #include <cmath> | ||
|
|
||
| #if __has_include(<execution>) | ||
| #include <thread> | ||
| #include <execution> | ||
| #endif | ||
|
|
||
| namespace boost { namespace math { namespace detail | ||
| { | ||
|
|
@@ -23,7 +27,7 @@ namespace boost { namespace math { namespace detail | |
| template<class Z, class Container> | ||
| void linear_sieve(Z upper_bound, Container &c) | ||
|
||
| { | ||
| Z least_divisors_size{upper_bound + 1}; | ||
| size_t least_divisors_size{static_cast<size_t>(upper_bound + 1)}; | ||
| Z *least_divisors{new Z[least_divisors_size]{0}}; | ||
mborland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (Z i{2}; i <= upper_bound; ++i) | ||
|
|
@@ -123,44 +127,57 @@ void mask_sieve(Z lower_bound, Z upper_bound, Container &c) | |
| } | ||
| } // End namespace detail | ||
|
|
||
| template<typename Z, class OutputIterator> | ||
| auto prime_sieve(Z upper_bound, OutputIterator output) -> decltype(output) | ||
| #if __has_include(<execution>) | ||
| template<class ExecutionPolicy, typename Z, class OutputIterator> | ||
| auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) -> decltype(output) | ||
| { | ||
| static_assert(std::is_integral<Z>::value, "No primes for floating point types"); | ||
| BOOST_ASSERT_MSG(upper_bound + 1 < std::numeric_limits<Z>::max(), "Type Overflow"); | ||
|
|
||
| std::vector<Z> primes; | ||
| primes.reserve(upper_bound / std::log(upper_bound)); | ||
|
|
||
| if (upper_bound <= 104729) | ||
| if (upper_bound <= 8192) | ||
| { | ||
| boost::math::detail::prime_table(upper_bound, primes); | ||
| boost::math::detail::linear_sieve(upper_bound, primes); | ||
| } | ||
|
|
||
| else | ||
| { | ||
| std::vector<Z> small_primes; | ||
| small_primes.reserve(1000); | ||
|
|
||
| // Spilt into two vectors and merge after joined to avoid data races | ||
| std::thread t1([upper_bound, &small_primes]{boost::math::detail::prime_table(static_cast<Z>(104729), small_primes);}); | ||
| std::thread t2([upper_bound, &primes]{boost::math::detail::mask_sieve(static_cast<Z>(104729), upper_bound, primes);}); | ||
| if constexpr (std::is_same_v<decltype(policy), std::execution::sequenced_policy>) | ||
| { | ||
| boost::math::detail::mask_sieve(static_cast<Z>(2), upper_bound, primes); | ||
| } | ||
|
|
||
| t1.join(); | ||
| t2.join(); | ||
| primes.insert(primes.begin(), small_primes.begin(), small_primes.end()); | ||
| else | ||
| { | ||
| std::vector<Z> small_primes; | ||
| small_primes.reserve(1000); | ||
|
|
||
| // Split into two vectors and merge after joined to avoid data races | ||
mborland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::thread t1([upper_bound, &small_primes] { | ||
| boost::math::detail::prime_table(static_cast<Z>(8192), small_primes); | ||
| }); | ||
| std::thread t2([upper_bound, &primes] { | ||
| boost::math::detail::mask_sieve(static_cast<Z>(8192), upper_bound, primes); | ||
| }); | ||
|
|
||
| t1.join(); | ||
| t2.join(); | ||
| primes.insert(primes.begin(), small_primes.begin(), small_primes.end()); | ||
| } | ||
| } | ||
|
|
||
| return std::move(primes.begin(), primes.end(), output); | ||
| } | ||
|
|
||
| template<class Z, class OutputIterator> | ||
| auto prime_range(Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) | ||
| template<class ExecutionPolicy, class Z, class OutputIterator> | ||
| auto prime_range(ExecutionPolicy&& policy, Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) | ||
| { | ||
| std::vector<Z> primes; | ||
| primes.reserve(upper_bound / std::log(static_cast<double>(upper_bound))); | ||
|
|
||
| boost::math::prime_sieve(upper_bound, std::back_inserter(primes)); | ||
| boost::math::prime_sieve(policy, upper_bound, std::back_inserter(primes)); | ||
|
|
||
| auto it{primes.begin()}; | ||
| while(*it < lower_bound && it != primes.end()) | ||
|
|
@@ -170,11 +187,45 @@ auto prime_range(Z lower_bound, Z upper_bound, OutputIterator output) -> decltyp | |
|
|
||
| return std::move(it, primes.end(), output); | ||
| } | ||
| #endif //__has_include(<execution>) | ||
|
|
||
| template<typename Z, class OutputIterator> | ||
| auto prime_sieve(Z upper_bound, OutputIterator output) -> decltype(output) | ||
| { | ||
| static_assert(std::is_integral<Z>::value, "No primes for floating point types"); | ||
| BOOST_ASSERT_MSG(upper_bound + 1 < std::numeric_limits<Z>::max(), "Type Overflow"); | ||
|
|
||
| std::vector<Z> primes; | ||
| primes.reserve(upper_bound / std::log(upper_bound)); | ||
|
|
||
| if (upper_bound <= 8192) | ||
| { | ||
| boost::math::detail::linear_sieve(upper_bound, primes); | ||
| } | ||
|
|
||
| else | ||
| { | ||
| boost::math::detail::mask_sieve(static_cast<Z>(2), upper_bound, primes); | ||
| } | ||
|
|
||
| return std::move(primes.begin(), primes.end(), output); | ||
| } | ||
mborland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| template<class Z, class OutputIterator> | ||
| inline auto prime_range(Z upper_bound, OutputIterator output) -> decltype(output) | ||
| auto prime_range(Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) | ||
|
||
| { | ||
| return prime_range(static_cast<Z>(2), upper_bound, output); | ||
| std::vector<Z> primes; | ||
| primes.reserve(upper_bound / std::log(static_cast<double>(upper_bound))); | ||
|
|
||
| boost::math::prime_sieve(upper_bound, std::back_inserter(primes)); | ||
|
|
||
| auto it{primes.begin()}; | ||
| while(*it < lower_bound && it != primes.end()) | ||
| { | ||
| ++it; | ||
| } | ||
|
|
||
| return std::move(it, primes.end(), output); | ||
| } | ||
| }} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally wouldn't use Z, since it is the name of an infinite set of numbers; it doesn't really fit into a taxonomy of concepts. (And no computer integer type is infinite.) It fits into the taxonomy of numbers, in which there are various supersets of it such as R and Z[i], but only integer-like numbers (such as Z[i] but not R) will work here. Concepts can specify these properties, which is why it is important to use them as opposed to names of numbers.
So, the appropriate concept here is probably DiscreteArchimedeanRing, but no-one knows what that means unless they have memorized Elements of Programming. So everyone just writes Integer. :)
Now you might say, but that's what Z means! It's a subtle distinction, sure. "Z" is a set, but "Integer" is a concept.
And don't even get me started on R.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With C++20 concepts would you use
template<Integer Z, class Container>? I can see for now replacing Z with Integer making sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't spent much time thinking about how I would name template parameters using the concepts syntax in C++20, sorry. :)