Merge branch 'master' of github.com:mpusz/units

This commit is contained in:
Mateusz Pusz
2024-11-16 08:07:33 +01:00
5 changed files with 338 additions and 169 deletions

View File

@ -31,9 +31,6 @@ import mp_units;
using namespace units;
using namespace units::detail;
template<>
constexpr std::optional<std::intmax_t> units::known_first_factor<9223372036854775783> = 9223372036854775783;
namespace {
// A set of non-standard bases for testing purposes.
@ -182,20 +179,6 @@ static_assert(std::is_same_v<decltype(get_base(power_v<mag_2, 5, 8>{})), mag_2_>
// mag_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.
// mag<9'223'372'036'854'775'783>();
// }
// }
// TEST_CASE("magnitude converts to numerical value")
// {
// SECTION("Positive integer powers of integer bases give integer values")

View File

@ -35,51 +35,6 @@ namespace {
inline constexpr auto MAX_U64 = std::numeric_limits<std::uint64_t>::max();
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(109'561) == is_prime_by_trial_division(109'561));
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));
// Modular arithmetic.
static_assert(add_mod(1u, 2u, 5u) == 3u);
static_assert(add_mod(4u, 4u, 5u) == 3u);
@ -122,4 +77,103 @@ static_assert(miller_rabin_probable_prime(2u, 9'007'199'254'740'881u), "Large kn
static_assert(miller_rabin_probable_prime(2u, 18'446'744'073'709'551'557u), "Largest 64-bit prime");
// Jacobi symbols --- a building block for the Strong Lucas probable prime test, needed for Baillie-PSW.
static_assert(jacobi_symbol(1, 1u) == 1, "Jacobi symbol always 1 when 'numerator' is 1");
static_assert(jacobi_symbol(1, 3u) == 1, "Jacobi symbol always 1 when 'numerator' is 1");
static_assert(jacobi_symbol(1, 5u) == 1, "Jacobi symbol always 1 when 'numerator' is 1");
static_assert(jacobi_symbol(1, 987654321u) == 1, "Jacobi symbol always 1 when 'numerator' is 1");
static_assert(jacobi_symbol(3, 1u) == 1, "Jacobi symbol always 1 when 'denominator' is 1");
static_assert(jacobi_symbol(5, 1u) == 1, "Jacobi symbol always 1 when 'denominator' is 1");
static_assert(jacobi_symbol(-1234567890, 1u) == 1, "Jacobi symbol always 1 when 'denominator' is 1");
static_assert(jacobi_symbol(10, 5u) == 0, "Jacobi symbol always 0 when there's a common factor");
static_assert(jacobi_symbol(25, 15u) == 0, "Jacobi symbol always 0 when there's a common factor");
static_assert(jacobi_symbol(-24, 9u) == 0, "Jacobi symbol always 0 when there's a common factor");
static_assert(jacobi_symbol(14, 9u) == +jacobi_symbol(7, 9u),
"Divide numerator by 2: positive when (denom % 8) in {1, 7}");
static_assert(jacobi_symbol(14, 15u) == +jacobi_symbol(7, 15u),
"Divide numerator by 2: positive when (denom % 8) in {1, 7}");
static_assert(jacobi_symbol(14, 11u) == -jacobi_symbol(7, 11u),
"Divide numerator by 2: negative when (denom % 8) in {3, 5}");
static_assert(jacobi_symbol(14, 13u) == -jacobi_symbol(7, 13u),
"Divide numerator by 2: negative when (denom % 8) in {3, 5}");
static_assert(jacobi_symbol(19, 9u) == +jacobi_symbol(9, 19u), "Flip is identity when (n % 4) = 1");
static_assert(jacobi_symbol(17, 7u) == +jacobi_symbol(7, 17u), "Flip is identity when (a % 4) = 1");
static_assert(jacobi_symbol(19, 7u) == -jacobi_symbol(9, 7u), "Flip changes sign when (n % 4) = 3 and (a % 4) = 3");
static_assert(jacobi_symbol(1001, 9907u) == -1, "Example from Wikipedia page");
static_assert(jacobi_symbol(19, 45u) == 1, "Example from Wikipedia page");
static_assert(jacobi_symbol(8, 21u) == -1, "Example from Wikipedia page");
static_assert(jacobi_symbol(5, 21u) == 1, "Example from Wikipedia page");
// Tests for perfect square finder
static_assert(is_perfect_square(0u));
static_assert(is_perfect_square(1u));
static_assert(!is_perfect_square(2u));
static_assert(is_perfect_square(4u));
constexpr std::uint64_t BIG_SQUARE = [](auto x) { return x * x; }((std::uint64_t{1u} << 32) - 1u);
static_assert(!is_perfect_square(BIG_SQUARE - 1u));
static_assert(is_perfect_square(BIG_SQUARE));
static_assert(!is_perfect_square(BIG_SQUARE + 1u));
// Tests for the Strong Lucas Probable Prime test.
static_assert(as_int(LucasDParameter{.mag = 5, .pos = true}) == 5);
static_assert(as_int(LucasDParameter{.mag = 7, .pos = false}) == -7);
static_assert(as_int(LucasDParameter{}) == 5, "First D parameter in the sequence is 5");
static_assert(as_int(successor(LucasDParameter{})) == -7, "Incrementing adds 2 to the mag, and flips the sign");
static_assert(as_int(successor(successor(LucasDParameter{}))) == 9);
static_assert(as_int(successor(successor(successor(LucasDParameter{})))) == -11);
static_assert(strong_lucas_probable_prime(3u), "Known small prime");
static_assert(strong_lucas_probable_prime(5u), "Known small prime");
static_assert(strong_lucas_probable_prime(7u), "Known small prime");
static_assert(!strong_lucas_probable_prime(9u), "Known small composite");
// Test some Miller-Rabin pseudoprimes (https://oeis.org/A001262), which should NOT be marked prime.
static_assert(!strong_lucas_probable_prime(2047u), "Miller-Rabin pseudoprime");
static_assert(!strong_lucas_probable_prime(3277u), "Miller-Rabin pseudoprime");
static_assert(!strong_lucas_probable_prime(486737u), "Miller-Rabin pseudoprime");
// Test some Strong Lucas pseudoprimes (https://oeis.org/A217255).
static_assert(strong_lucas_probable_prime(5459u), "Strong Lucas pseudoprime");
static_assert(strong_lucas_probable_prime(5777u), "Strong Lucas pseudoprime");
static_assert(strong_lucas_probable_prime(10877u), "Strong Lucas pseudoprime");
static_assert(strong_lucas_probable_prime(324899u), "Strong Lucas pseudoprime");
// Test some actual primes
static_assert(strong_lucas_probable_prime(225'653'407'801u), "Large known prime");
static_assert(strong_lucas_probable_prime(334'524'384'739u), "Large known prime");
static_assert(strong_lucas_probable_prime(9'007'199'254'740'881u), "Large known prime");
static_assert(strong_lucas_probable_prime(18'446'744'073'709'551'557u), "Largest 64-bit prime");
// Tests for Baillie-PSW, which is known to be correct for all 64-bit integers.
static_assert(baillie_psw_probable_prime(3u), "Known small prime");
static_assert(baillie_psw_probable_prime(5u), "Known small prime");
static_assert(baillie_psw_probable_prime(7u), "Known small prime");
static_assert(!baillie_psw_probable_prime(9u), "Known small composite");
// Test some Miller-Rabin pseudoprimes (https://oeis.org/A001262), which should NOT be marked prime.
static_assert(!baillie_psw_probable_prime(2047u), "Miller-Rabin pseudoprime");
static_assert(!baillie_psw_probable_prime(3277u), "Miller-Rabin pseudoprime");
static_assert(!baillie_psw_probable_prime(486737u), "Miller-Rabin pseudoprime");
// Test some Strong Lucas pseudoprimes (https://oeis.org/A217255), which should NOT be marked prime.
static_assert(!baillie_psw_probable_prime(5459u), "Strong Lucas pseudoprime");
static_assert(!baillie_psw_probable_prime(5777u), "Strong Lucas pseudoprime");
static_assert(!baillie_psw_probable_prime(10877u), "Strong Lucas pseudoprime");
static_assert(!baillie_psw_probable_prime(324899u), "Strong Lucas pseudoprime");
// Test some actual primes
static_assert(baillie_psw_probable_prime(225'653'407'801u), "Large known prime");
static_assert(baillie_psw_probable_prime(334'524'384'739u), "Large known prime");
static_assert(baillie_psw_probable_prime(9'007'199'254'740'881u), "Large known prime");
static_assert(baillie_psw_probable_prime(18'446'744'073'709'551'557u), "Largest 64-bit prime");
} // namespace