From 543c7b941a37f3d855458121d5b7353c7d6a7cde Mon Sep 17 00:00:00 2001 From: nicoo Date: Sun, 24 May 2020 18:26:26 +0200 Subject: [PATCH] factor::rho: Small refactor --- src/uu/factor/src/rho.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/uu/factor/src/rho.rs b/src/uu/factor/src/rho.rs index f8adb4193..e864519c9 100644 --- a/src/uu/factor/src/rho.rs +++ b/src/uu/factor/src/rho.rs @@ -35,17 +35,22 @@ fn find_divisor(n: u64) -> u64 { } } -pub(crate) fn factor(mut num: u64) -> Factors { +fn _factor(mut num: u64) -> Factors { + // Shadow the name, so the recursion automatically goes from “Big” arithmetic to small. + let _factor = |n| { + if n < 1 << 63 { + _factor::(n) + } else { + _factor::(n) + } + }; + let mut factors = Factors::new(); if num == 1 { return factors; } - match if num < 1 << 63 { - miller_rabin::test::(num) - } else { - miller_rabin::test::(num) - } { + match miller_rabin::test::(num) { Prime => { factors.push(num); return factors; @@ -53,18 +58,22 @@ pub(crate) fn factor(mut num: u64) -> Factors { Composite(d) => { num /= d; - factors *= factor(d); + factors *= _factor(d) } Pseudoprime => {} }; - let divisor = if num < 1 << 63 { - find_divisor::(num) - } else { - find_divisor::(num) - }; - factors *= factor(divisor); - factors *= factor(num / divisor); + let divisor = find_divisor::(num); + factors *= _factor(divisor); + factors *= _factor(num / divisor); factors } + +pub(crate) fn factor(n: u64) -> Factors { + if n < 1 << 63 { + _factor::(n) + } else { + _factor::(n) + } +}