Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid large intermediate product in LCM #350

Merged
merged 1 commit into from
Dec 14, 2017
Merged

Conversation

mhogrefe
Copy link
Contributor

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.

@mhogrefe
Copy link
Contributor Author

Hmm, Travis has this failing on nightly but the error is rather mysterious and doesn't seem related to my change.

@hcpl
Copy link

hcpl commented Dec 14, 2017

Don't worry, the build is failing because the syntax API changed again, which is expected from nightlies. My PR #348 is suffering from this too.

@cuviper
Copy link
Member

cuviper commented Dec 14, 2017

Ugh, num-macros... I'm inclined to finally let that die.

This PR is good though -- we already did similar to avoid overflowing primitives in #167.

@cuviper
Copy link
Member

cuviper commented Dec 14, 2017

#351 nuked num-macros, so let's give this a try:

bors r+

bors bot added a commit that referenced this pull request Dec 14, 2017
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.
@bors
Copy link
Contributor

bors bot commented Dec 14, 2017

Build succeeded

@bors bors bot merged commit 56a029b into rust-num:master Dec 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants