-
Notifications
You must be signed in to change notification settings - Fork 94
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
Use wheel factorization for prime numbers #337
Merged
+304
−28
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
04b80f0
Use wheel factorization for prime numbers
chiphogg 24b284f
Add tests to support claims in comment
chiphogg c8a44ad
Add missing header for `std::integral`
chiphogg 6c73947
Satisfy complaint
chiphogg 73a5611
Fix wheel factorization algorithm
chiphogg bfa8db6
Use std::accumulate
chiphogg 59d9cd1
static_cast for first factor
chiphogg 70640a1
Remove constexpr-incompatible assert()
chiphogg a719a8b
Try upping the basis size
chiphogg 8707385
Try upping the basis size further
chiphogg 1e8460d
Revert "Try upping the basis size further"
chiphogg 28c4fe3
Run clang-format-15 on changed files
chiphogg a99e5f9
Switch tests to use top-level, anonymous namespace
chiphogg 166dd1e
Work around numbers with very large first factors
chiphogg 48b6280
Merge branch 'master' into chiphogg/prime-wheel
chiphogg c339383
Convert names to standard_case
chiphogg 0f80c10
Try "gentler" test case
chiphogg 438feb3
Remove offending unit test
chiphogg f495ad9
Replace `accumulate` with `reduce`
chiphogg 6872117
Replace `reduce` with bespoke implementation
chiphogg b589ba8
Omit redundant computation
chiphogg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2018 Mateusz Pusz | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <cstddef> | ||
#include <numeric> | ||
#include <optional> | ||
#include <tuple> | ||
|
||
namespace units::detail { | ||
|
||
constexpr bool is_prime_by_trial_division(std::size_t n) | ||
{ | ||
for (std::size_t f = 2; f * f <= n; f += 1 + (f % 2)) { | ||
if (n % f == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// Return the first factor of n, as long as it is either k or n. | ||
// | ||
// Precondition: no integer smaller than k evenly divides n. | ||
constexpr std::optional<std::size_t> first_factor_maybe(std::size_t n, std::size_t k) | ||
{ | ||
if (n % k == 0) { | ||
return k; | ||
} | ||
if (k * k > n) { | ||
return n; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
template<std::size_t N> | ||
constexpr std::array<std::size_t, N> first_n_primes() | ||
{ | ||
std::array<std::size_t, N> primes; | ||
primes[0] = 2; | ||
for (std::size_t i = 1; i < N; ++i) { | ||
primes[i] = primes[i - 1] + 1; | ||
while (!is_prime_by_trial_division(primes[i])) { | ||
++primes[i]; | ||
} | ||
} | ||
return primes; | ||
} | ||
|
||
template<std::size_t N, typename Callable> | ||
constexpr void call_for_coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis, Callable&& call) | ||
{ | ||
for (std::size_t i = 0u; i < n; ++i) { | ||
if (std::apply([&i](auto... primes) { return ((i % primes != 0) && ...); }, basis)) { | ||
call(i); | ||
} | ||
} | ||
} | ||
|
||
template<std::size_t N> | ||
constexpr std::size_t num_coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis) | ||
{ | ||
std::size_t count = 0u; | ||
call_for_coprimes_up_to(n, basis, [&count](auto) { ++count; }); | ||
return count; | ||
} | ||
|
||
template<std::size_t ResultSize, std::size_t N> | ||
constexpr auto coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis) | ||
{ | ||
std::array<std::size_t, ResultSize> coprimes; | ||
std::size_t i = 0u; | ||
|
||
call_for_coprimes_up_to(n, basis, [&coprimes, &i](std::size_t cp) { coprimes[i++] = cp; }); | ||
|
||
return coprimes; | ||
} | ||
|
||
template<std::size_t N> | ||
constexpr std::size_t product(const std::array<std::size_t, N>& values) | ||
{ | ||
std::size_t product = 1; | ||
for (const auto& v : values) { | ||
product *= v; | ||
} | ||
return product; | ||
} | ||
|
||
// A configurable instantiation of the "wheel factorization" algorithm [1] for prime numbers. | ||
// | ||
// Instantiate with N to use a "basis" of the first N prime numbers. Higher values of N use fewer trial divisions, at | ||
// the cost of additional space. The amount of space consumed is roughly the total number of numbers that are a) less | ||
// than the _product_ of the basis elements (first N primes), and b) coprime with every element of the basis. This | ||
// means it grows rapidly with N. Consider this approximate chart: | ||
// | ||
// N | Num coprimes | Trial divisions needed | ||
// --+--------------+----------------------- | ||
// 1 | 1 | 50.0 % | ||
// 2 | 2 | 33.3 % | ||
// 3 | 8 | 26.7 % | ||
// 4 | 48 | 22.9 % | ||
// 5 | 480 | 20.8 % | ||
// | ||
// Note the diminishing returns, and the rapidly escalating costs. Consider this behaviour when choosing the value of N | ||
// most appropriate for your needs. | ||
// | ||
// [1] https://en.wikipedia.org/wiki/Wheel_factorization | ||
template<std::size_t BasisSize> | ||
struct wheel_factorizer { | ||
static constexpr auto basis = first_n_primes<BasisSize>(); | ||
static constexpr std::size_t wheel_size = product(basis); | ||
static constexpr auto coprimes_in_first_wheel = | ||
coprimes_up_to<num_coprimes_up_to(wheel_size, basis)>(wheel_size, basis); | ||
|
||
static constexpr std::size_t find_first_factor(std::size_t n) | ||
{ | ||
for (const auto& p : basis) { | ||
if (const auto k = first_factor_maybe(n, p)) { | ||
return *k; | ||
} | ||
} | ||
|
||
for (auto it = std::next(std::begin(coprimes_in_first_wheel)); it != std::end(coprimes_in_first_wheel); ++it) { | ||
if (const auto k = first_factor_maybe(n, *it)) { | ||
return *k; | ||
} | ||
} | ||
Comment on lines
+145
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
||
for (std::size_t wheel = wheel_size; wheel < n; wheel += wheel_size) { | ||
for (const auto& p : coprimes_in_first_wheel) { | ||
if (const auto k = first_factor_maybe(n, wheel + p)) { | ||
return *k; | ||
} | ||
} | ||
Comment on lines
+152
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} | ||
|
||
return n; | ||
} | ||
|
||
static constexpr bool is_prime(std::size_t n) { return (n > 1) && find_first_factor(n) == n; } | ||
}; | ||
|
||
} // namespace units::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please use
detail::find_if()
and provide a TODO comment. See more in: e1f7266.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.
If I do that, how will I know which value to return without calling
first_factor_maybe()
again? Granted, that's not very expensive, but it seems a little inelegant.In more detail: I guess this would look like:
Are we sure that's better?
In the existing code, I can see the nuisance of repeating the
if
block four times. The best solution that occurs to me would be to separate the value generation from the evaluation---maybe have some kind of iterator interface which returns the basis primes in order, then first-wheel coprimes (except 1), then coprimes in order for each wheel indefinitely. I assume that's doable with something like ranges or coroutines, although I'm not very well versed in either. I think it would be fine to leave as a future refactoring.That said---I did realize in grappling with this comment that one of the four repetitions was redundant! It stems from my earlier mistake of using primes; 1 is not a prime, but it is coprime, so we were checking it twice per wheel. That's fixed now.
Let me know your thoughts on how to proceed here: status quo, or a
find_if
solution.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.
Also, to be clear about why "which value to return" is even an issue:
first_factor_maybe
can return eitherk
itself, orn
(ifk * k
exceedsn
).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.
Right, initially I thought it will be easier to simplify... Sorry, for causing confusion.
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.
However, it looks like the same pattern in all cases so maybe a custom algorithm can be provided to cover those?
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 want that too, but I think
first_factor_maybe()
is that algorithm---at least, as much of it as I was able to pull out. I think the next level of refactoring would be to separate out the trial number generation, probably placing it behind some kind of iterator interface. Then we could just keep taking values until we find one that works, and we'd have only a single call tofirst_factor_maybe()
(or more likely, we'd just get rid of that function at that point).If you have a ready made solution for this, I'd love to add it! Otherwise, I think the code is reasonably clean and efficient for a first pass.