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.
This commit is contained in:
bors[bot] 2017-12-14 08:07:15 +00:00
commit f172ef3a6b
1 changed files with 1 additions and 1 deletions

View File

@ -955,7 +955,7 @@ impl Integer for BigUint {
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
#[inline]
fn lcm(&self, other: &BigUint) -> BigUint {
((self * other) / self.gcd(other))
self / self.gcd(other) * other
}
/// Deprecated, use `is_multiple_of` instead.