73 auto f = [&](
const uint64_t val) {
74 const uint64_t v2 =
mod_mul(val, val, n);
75# if defined(__SIZEOF_INT128__) && !defined(_WIN32)
76 return static_cast<uint64_t
>((
static_cast<__uint128_t
>(v2) + c) % n);
79 uint64_t c_mod = c % n;
80 if (n - v2 <= c_mod)
return c_mod - (n - v2);
88 constexpr size_t max_iterations = 1000000;
89 for (
size_t iter = 0; iter < max_iterations
and d == 1; ++iter)
93 const uint64_t
diff = x >
y ? x -
y :
y - x;
94 d = std::gcd(
diff, n);
115 if (n % 2 == 0)
return 2;
116 if (n % 3 == 0)
return 3;
117 if (n % 5 == 0)
return 5;
120 thread_local std::mt19937_64
rng([]() {
121 std::random_device rd;
125 constexpr size_t max_attempts = 256;
126 for (
size_t attempt = 0; attempt < max_attempts; ++attempt)
128 const uint64_t
seed =
rng() % (n - 2) + 2;
129 const uint64_t c =
rng() % (n - 1) + 1;
135 <<
"pollard_rho: failed to find a factor of " << n
136 <<
" after " << max_attempts <<
" attempts";
138# if defined(__GNUC__) || defined(__clang__)
139 __builtin_unreachable();
140# elif defined(_MSC_VER)
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
High-level sorting functions for Aleph containers.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
uint64_t pollard_rho_step(const uint64_t n, const uint64_t seed, const uint64_t c)
Inner function for Pollard's rho algorithm.
void extract_prime_factors(const uint64_t n, Array< uint64_t > &factors)
Recursive extraction of all prime factors.
uint64_t find_any_factor(const uint64_t n)
Helper to repeatedly apply Pollard's rho until a factor is found.
Main namespace for Aleph-w library functions.
Array< uint64_t > pollard_rho(uint64_t n)
Compute the prime factorization of a number using Pollard's rho.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
and
Check uniqueness with explicit hash + equality functors.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
Advanced primality testing algorithms.
Dynamic array container with automatic resizing.