mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 20:47:46 +00:00
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.
This commit is contained in:
parent
9eb944b6b9
commit
9fe3de72f2
1 changed files with 6 additions and 5 deletions
|
@ -48,7 +48,7 @@ fn find_divisor<A: Arithmetic>(n: A) -> u64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _factor<A: Arithmetic>(mut num: u64) -> Factors {
|
fn _factor<A: Arithmetic>(num: u64) -> Factors {
|
||||||
// Shadow the name, so the recursion automatically goes from “Big” arithmetic to small.
|
// Shadow the name, so the recursion automatically goes from “Big” arithmetic to small.
|
||||||
let _factor = |n| {
|
let _factor = |n| {
|
||||||
// TODO: Optimise with 32 and 64b versions
|
// TODO: Optimise with 32 and 64b versions
|
||||||
|
@ -61,6 +61,7 @@ fn _factor<A: Arithmetic>(mut num: u64) -> Factors {
|
||||||
}
|
}
|
||||||
|
|
||||||
let n = A::new(num);
|
let n = A::new(num);
|
||||||
|
let divisor;
|
||||||
match miller_rabin::test::<A>(n) {
|
match miller_rabin::test::<A>(n) {
|
||||||
Prime => {
|
Prime => {
|
||||||
factors.push(num);
|
factors.push(num);
|
||||||
|
@ -68,14 +69,14 @@ fn _factor<A: Arithmetic>(mut num: u64) -> Factors {
|
||||||
}
|
}
|
||||||
|
|
||||||
Composite(d) => {
|
Composite(d) => {
|
||||||
num /= d;
|
divisor = d;
|
||||||
factors *= _factor(d)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Pseudoprime => {}
|
Pseudoprime => {
|
||||||
|
divisor = find_divisor::<A>(n);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let divisor = find_divisor::<A>(n);
|
|
||||||
factors *= _factor(divisor);
|
factors *= _factor(divisor);
|
||||||
factors *= _factor(num / divisor);
|
factors *= _factor(num / divisor);
|
||||||
factors
|
factors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue