Merge pull request #337 from chiphogg/chiphogg/prime-wheel
Use wheel factorization for prime numbers
This commit is contained in:
5 files changed
+304
-28
No files matched your search
@@ -25,7 +25,13 @@
|
||||
#include <units/ratio.h>
|
||||
#include <type_traits>
|
||||
|
||||
namespace units {
|
||||
using namespace units;
|
||||
using namespace units::detail;
|
||||
|
||||
template<>
|
||||
inline constexpr std::optional<std::intmax_t> units::known_first_factor<9223372036854775783> = 9223372036854775783;
|
||||
|
||||
namespace {
|
||||
|
||||
// A set of non-standard bases for testing purposes.
|
||||
struct noninteger_base {
|
||||
@@ -149,6 +155,17 @@ TEST_CASE("make_ratio performs prime factorization correctly")
|
||||
// The failure was due to a prime factor which is larger than 2^31.
|
||||
as_magnitude<ratio(16'605'390'666'050, 10'000'000'000'000)>();
|
||||
}
|
||||
|
||||
SECTION("Can bypass computing primes by providing known_first_factor<N>")
|
||||
{
|
||||
// Sometimes, even wheel factorization isn't enough to handle the compilers' limits on constexpr steps and/or
|
||||
// iterations. To work around these cases, we can explicitly provide the correct answer directly to the compiler.
|
||||
//
|
||||
// In this case, we test that we can represent the largest prime that fits in a signed 64-bit int. The reason this
|
||||
// test can pass is that we have provided the answer, by specializing the `known_first_factor` variable template
|
||||
// above in this file.
|
||||
as_magnitude<9'223'372'036'854'775'783>();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("magnitude converts to numerical value")
|
||||
@@ -334,7 +351,8 @@ TEST_CASE("can distinguish integral, rational, and irrational magnitudes")
|
||||
}
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Detail function tests below.
|
||||
|
||||
TEST_CASE("int_power computes integer powers")
|
||||
{
|
||||
@@ -358,16 +376,6 @@ TEST_CASE("int_power computes integer powers")
|
||||
|
||||
TEST_CASE("Prime helper functions")
|
||||
{
|
||||
SECTION("find_first_factor()")
|
||||
{
|
||||
CHECK(find_first_factor(1) == 1);
|
||||
CHECK(find_first_factor(2) == 2);
|
||||
CHECK(find_first_factor(4) == 2);
|
||||
CHECK(find_first_factor(6) == 2);
|
||||
CHECK(find_first_factor(15) == 3);
|
||||
CHECK(find_first_factor(17) == 17);
|
||||
}
|
||||
|
||||
SECTION("multiplicity")
|
||||
{
|
||||
CHECK(multiplicity(2, 8) == 3);
|
||||
@@ -514,6 +522,4 @@ TEST_CASE("strictly_increasing")
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace units
|
||||
} // namespace
|
||||
@@ -52,6 +52,7 @@ add_library(unit_tests_static
|
||||
kind_test.cpp
|
||||
math_test.cpp
|
||||
point_origin_test.cpp
|
||||
prime_test.cpp
|
||||
ratio_test.cpp
|
||||
references_test.cpp
|
||||
si_test.cpp
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// 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.
|
||||
|
||||
#include <units/bits/prime.h>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
using namespace units::detail;
|
||||
|
||||
namespace {
|
||||
|
||||
template<std::size_t BasisSize, std::size_t... Is>
|
||||
constexpr bool check_primes(std::index_sequence<Is...>)
|
||||
{
|
||||
return ((Is < 2 || wheel_factorizer<BasisSize>::is_prime(Is) == is_prime_by_trial_division(Is)) && ...);
|
||||
}
|
||||
|
||||
static_assert(check_primes<2>(std::make_index_sequence<122>{}));
|
||||
|
||||
// This is the smallest number that can catch the bug where we use only _prime_ numbers in the first wheel, rather than
|
||||
// numbers which are _coprime to the basis_.
|
||||
//
|
||||
// The basis for N = 4 is {2, 3, 5, 7}, so the wheel size is 210. 11 * 11 = 121 is within the first wheel. It is
|
||||
// coprime with every element of the basis, but it is _not_ prime. If we keep only prime numbers, then we will neglect
|
||||
// using numbers of the form (210 * n + 121) as trial divisors, which is a problem if any are prime. For n = 1, we have
|
||||
// a divisor of (210 + 121 = 331), which happens to be prime but will not be used. Thus, (331 * 331 = 109561) is a
|
||||
// composite number which could wrongly appear prime if we skip over 331.
|
||||
static_assert(wheel_factorizer<4>::is_prime(109561) == is_prime_by_trial_division(109561));
|
||||
|
||||
static_assert(wheel_factorizer<1>::coprimes_in_first_wheel.size() == 1);
|
||||
static_assert(wheel_factorizer<2>::coprimes_in_first_wheel.size() == 2);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel.size() == 8);
|
||||
static_assert(wheel_factorizer<4>::coprimes_in_first_wheel.size() == 48);
|
||||
static_assert(wheel_factorizer<5>::coprimes_in_first_wheel.size() == 480);
|
||||
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[0] == 1);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[1] == 7);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[2] == 11);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[3] == 13);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[4] == 17);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[5] == 19);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[6] == 23);
|
||||
static_assert(wheel_factorizer<3>::coprimes_in_first_wheel[7] == 29);
|
||||
|
||||
static_assert(!wheel_factorizer<1>::is_prime(0));
|
||||
static_assert(!wheel_factorizer<1>::is_prime(1));
|
||||
static_assert(wheel_factorizer<1>::is_prime(2));
|
||||
|
||||
static_assert(!wheel_factorizer<2>::is_prime(0));
|
||||
static_assert(!wheel_factorizer<2>::is_prime(1));
|
||||
static_assert(wheel_factorizer<2>::is_prime(2));
|
||||
|
||||
static_assert(!wheel_factorizer<3>::is_prime(0));
|
||||
static_assert(!wheel_factorizer<3>::is_prime(1));
|
||||
static_assert(wheel_factorizer<3>::is_prime(2));
|
||||
|
||||
} // namespace
|
||||
Reference in new issue
Block a user