Skip to content

Commit

Permalink
fix/factor ~ fix fault when factoring number composed of a squared fa…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
rivy committed Oct 26, 2020
1 parent 8593b4c commit 368f473
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/uu/factor/src/factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,16 @@ pub fn factor(num: u64) -> Factors {

if let Some(divisor) = find_factor(factor) {
let mut gcd_queue = Decomposition::one();
gcd_queue.add(divisor, exp);
gcd_queue.add(factor / divisor, exp);

let mut non_trivial_gcd = true;
let quotient = factor / divisor;
if quotient == divisor {
gcd_queue.add(divisor, exp + 1);
} else {
gcd_queue.add(divisor, exp);
gcd_queue.add(quotient, exp);
}

let mut non_trivial_gcd = quotient != divisor;
while non_trivial_gcd {
debug_assert_eq!(factor, gcd_queue.product());

Expand Down

0 comments on commit 368f473

Please sign in to comment.