From 9fe3de72f23093ec61e0420403e85a091ced98fe Mon Sep 17 00:00:00 2001 From: nicoo Date: Fri, 19 Jun 2020 13:51:29 +0200 Subject: [PATCH] factor::rho: Fix very-unlikely bug (resulting in assertion failure) This bug can only be triggered when: - the Miller-Rabin test produces a divisor `d` (rare) ; - n / d is prime. --- src/uu/factor/src/rho.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/uu/factor/src/rho.rs b/src/uu/factor/src/rho.rs index f15ef5468..e9076144b 100644 --- a/src/uu/factor/src/rho.rs +++ b/src/uu/factor/src/rho.rs @@ -48,7 +48,7 @@ fn find_divisor(n: A) -> u64 { } } -fn _factor(mut num: u64) -> Factors { +fn _factor(num: u64) -> Factors { // Shadow the name, so the recursion automatically goes from “Big” arithmetic to small. let _factor = |n| { // TODO: Optimise with 32 and 64b versions @@ -61,6 +61,7 @@ fn _factor(mut num: u64) -> Factors { } let n = A::new(num); + let divisor; match miller_rabin::test::(n) { Prime => { factors.push(num); @@ -68,14 +69,14 @@ fn _factor(mut num: u64) -> Factors { } Composite(d) => { - num /= d; - factors *= _factor(d) + divisor = d; } - Pseudoprime => {} + Pseudoprime => { + divisor = find_divisor::(n); + } }; - let divisor = find_divisor::(n); factors *= _factor(divisor); factors *= _factor(num / divisor); factors