1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

factor/refactor ~ fix cargo clippy complaints (allow many_single_char_names)

This commit is contained in:
Roy Ivy III 2020-10-07 21:21:14 -05:00
parent 0d39732300
commit 3bb3080170

View file

@ -140,10 +140,10 @@ pub fn factor(num: u64) -> Factors {
} }
let mut n = num; let mut n = num;
let z = num.trailing_zeros(); let n_zeros = num.trailing_zeros();
if z > 0 { if n_zeros > 0 {
factors.add(2, z as Exponent); factors.add(2, n_zeros as Exponent);
n >>= z; n >>= n_zeros;
} }
debug_assert_eq!(num, n * factors.product()); debug_assert_eq!(num, n * factors.product());
@ -165,22 +165,22 @@ pub fn factor(num: u64) -> Factors {
// Check correctness invariant // Check correctness invariant
debug_assert_eq!(num, factors.product() * dec.product()); debug_assert_eq!(num, factors.product() * dec.product());
let (f, e) = dec.pop().unwrap(); let (factor, exp) = dec.pop().unwrap();
if let Some(d) = find_factor(f) { if let Some(divisor) = find_factor(factor) {
let mut gcd_queue = Decomposition::one(); let mut gcd_queue = Decomposition::one();
gcd_queue.add(d, e); gcd_queue.add(divisor, exp);
gcd_queue.add(f / d, e); gcd_queue.add(factor / divisor, exp);
let mut non_trivial_gcd = true; let mut non_trivial_gcd = true;
while non_trivial_gcd { while non_trivial_gcd {
debug_assert_eq!(f, gcd_queue.product()); debug_assert_eq!(factor, gcd_queue.product());
let mut tmp = Decomposition::one(); let mut tmp = Decomposition::one();
non_trivial_gcd = false; non_trivial_gcd = false;
for i in 0..gcd_queue.0.len() - 1 { for i in 0..gcd_queue.0.len() - 1 {
let (mut a, e_a) = gcd_queue.0[i]; let (mut a, exp_a) = gcd_queue.0[i];
let (mut b, e_b) = gcd_queue.0[i + 1]; let (mut b, exp_b) = gcd_queue.0[i + 1];
if a == 1 { if a == 1 {
continue; continue;
@ -193,26 +193,26 @@ pub fn factor(num: u64) -> Factors {
b /= g; b /= g;
} }
if a != 1 { if a != 1 {
tmp.add(a, e_a); tmp.add(a, exp_a);
} }
if g != 1 { if g != 1 {
tmp.add(g, e_a + e_b); tmp.add(g, exp_a + exp_b);
} }
if i + 1 != gcd_queue.0.len() - 1 { if i + 1 != gcd_queue.0.len() - 1 {
gcd_queue.0[i + 1].0 = b; gcd_queue.0[i + 1].0 = b;
} else if b != 1 { } else if b != 1 {
tmp.add(b, e_b); tmp.add(b, exp_b);
} }
} }
gcd_queue = tmp; gcd_queue = tmp;
} }
debug_assert_eq!(f, gcd_queue.product()); debug_assert_eq!(factor, gcd_queue.product());
dec.0.extend(gcd_queue.0); dec.0.extend(gcd_queue.0);
} else { } else {
// f is prime // factor is prime
factors.add(f, e); factors.add(factor, exp);
} }
} }