mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
factor::rho: Small refactor
This commit is contained in:
parent
30fd6a0309
commit
543c7b941a
1 changed files with 23 additions and 14 deletions
|
@ -35,17 +35,22 @@ fn find_divisor<A: Arithmetic>(n: u64) -> u64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn factor(mut num: u64) -> Factors {
|
fn _factor<A: Arithmetic>(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::<Small>(n)
|
||||||
|
} else {
|
||||||
|
_factor::<A>(n)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let mut factors = Factors::new();
|
let mut factors = Factors::new();
|
||||||
if num == 1 {
|
if num == 1 {
|
||||||
return factors;
|
return factors;
|
||||||
}
|
}
|
||||||
|
|
||||||
match if num < 1 << 63 {
|
match miller_rabin::test::<A>(num) {
|
||||||
miller_rabin::test::<Small>(num)
|
|
||||||
} else {
|
|
||||||
miller_rabin::test::<Big>(num)
|
|
||||||
} {
|
|
||||||
Prime => {
|
Prime => {
|
||||||
factors.push(num);
|
factors.push(num);
|
||||||
return factors;
|
return factors;
|
||||||
|
@ -53,18 +58,22 @@ pub(crate) fn factor(mut num: u64) -> Factors {
|
||||||
|
|
||||||
Composite(d) => {
|
Composite(d) => {
|
||||||
num /= d;
|
num /= d;
|
||||||
factors *= factor(d);
|
factors *= _factor(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
Pseudoprime => {}
|
Pseudoprime => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
let divisor = if num < 1 << 63 {
|
let divisor = find_divisor::<A>(num);
|
||||||
find_divisor::<Small>(num)
|
factors *= _factor(divisor);
|
||||||
} else {
|
factors *= _factor(num / divisor);
|
||||||
find_divisor::<Big>(num)
|
|
||||||
};
|
|
||||||
factors *= factor(divisor);
|
|
||||||
factors *= factor(num / divisor);
|
|
||||||
factors
|
factors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn factor(n: u64) -> Factors {
|
||||||
|
if n < 1 << 63 {
|
||||||
|
_factor::<Small>(n)
|
||||||
|
} else {
|
||||||
|
_factor::<Big>(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue