Skip to content

Commit

Permalink
Guarantee return value is prime
Browse files Browse the repository at this point in the history
  • Loading branch information
chiphogg committed Nov 19, 2024
1 parent 46e3c54 commit 7b4de73
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions au/code/au/utility/factoring.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ constexpr std::uintmax_t find_pollard_rho_factor(std::uintmax_t n) {
++cycle_length;
factor = gcd(n, absolute_diff(tortoise, hare));
}
if (factor > 1u && factor < n) {
if (factor < n) {
return factor;
}
}
Expand Down Expand Up @@ -112,7 +112,11 @@ constexpr std::uintmax_t find_prime_factor(std::uintmax_t n) {
return n;
}

return find_pollard_rho_factor(n);
auto factor = find_pollard_rho_factor(n);
while (!is_prime(factor)) {
factor = find_pollard_rho_factor(factor);
}
return factor;
}

// Find the largest power of `factor` which divides `n`.
Expand Down

0 comments on commit 7b4de73

Please sign in to comment.