Skip to content

Commit f172ef3

Browse files
committed
Merge #350
350: Avoid large intermediate product in LCM r=cuviper a=mhogrefe Changed the implementation of BigUint LCM from `((self * other) / self.gcd(other))` to `self / self.gcd(other) * other` The division is exact in both cases, so the result is the same, but the new code avoids the potentially-large intermediate product, speeding things up and using less memory. I also removed the unnecessary parentheses, because I think it's clear what order everything will be executed in. But if others think differently I can add them back.
2 parents a0c431e + 56a029b commit f172ef3

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

bigint/src/biguint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ impl Integer for BigUint {
955955
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
956956
#[inline]
957957
fn lcm(&self, other: &BigUint) -> BigUint {
958-
((self * other) / self.gcd(other))
958+
self / self.gcd(other) * other
959959
}
960960

961961
/// Deprecated, use `is_multiple_of` instead.

0 commit comments

Comments
 (0)