mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
perf/factor ~ improve factor() quotient and loop comparison (~6% time improvement)
This commit is contained in:
parent
368f47381b
commit
6eea8c5f30
1 changed files with 5 additions and 5 deletions
|
@ -171,19 +171,19 @@ pub fn factor(num: u64) -> Factors {
|
||||||
let mut gcd_queue = Decomposition::one();
|
let mut gcd_queue = Decomposition::one();
|
||||||
|
|
||||||
let quotient = factor / divisor;
|
let quotient = factor / divisor;
|
||||||
if quotient == divisor {
|
let mut trivial_gcd = quotient == divisor;
|
||||||
|
if trivial_gcd {
|
||||||
gcd_queue.add(divisor, exp + 1);
|
gcd_queue.add(divisor, exp + 1);
|
||||||
} else {
|
} else {
|
||||||
gcd_queue.add(divisor, exp);
|
gcd_queue.add(divisor, exp);
|
||||||
gcd_queue.add(quotient, exp);
|
gcd_queue.add(quotient, exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut non_trivial_gcd = quotient != divisor;
|
while !trivial_gcd {
|
||||||
while non_trivial_gcd {
|
|
||||||
debug_assert_eq!(factor, gcd_queue.product());
|
debug_assert_eq!(factor, gcd_queue.product());
|
||||||
|
|
||||||
let mut tmp = Decomposition::one();
|
let mut tmp = Decomposition::one();
|
||||||
non_trivial_gcd = false;
|
trivial_gcd = true;
|
||||||
for i in 0..gcd_queue.0.len() - 1 {
|
for i in 0..gcd_queue.0.len() - 1 {
|
||||||
let (mut a, exp_a) = gcd_queue.0[i];
|
let (mut a, exp_a) = gcd_queue.0[i];
|
||||||
let (mut b, exp_b) = gcd_queue.0[i + 1];
|
let (mut b, exp_b) = gcd_queue.0[i + 1];
|
||||||
|
@ -194,7 +194,7 @@ pub fn factor(num: u64) -> Factors {
|
||||||
|
|
||||||
let g = gcd(a, b);
|
let g = gcd(a, b);
|
||||||
if g != 1 {
|
if g != 1 {
|
||||||
non_trivial_gcd = true;
|
trivial_gcd = false;
|
||||||
a /= g;
|
a /= g;
|
||||||
b /= g;
|
b /= g;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue