Skip to content

Instantly share code, notes, and snippets.

@HopefulLlama
Last active August 3, 2016 10:50
Show Gist options
  • Save HopefulLlama/94e768d482806ae681fa606e8d9b026f to your computer and use it in GitHub Desktop.
Save HopefulLlama/94e768d482806ae681fa606e8d9b026f to your computer and use it in GitHub Desktop.
Finds the Least Common Multiple for two numbers.
function getGreatestCommonDivisor(x, y) {
while(x !== 0 && y !== 0) {
if(x > y) {
x %= y;
} else {
y %= x;
}
}
return Math.max(x, y);
}
function getLeastCommonMultiple(x, y) {
return x / getGreatestCommonDivisor(x, y) * y;
}
@HopefulLlama
Copy link
Author

Sourced from John D Cook's answer to a StackOverflow question at:
http://stackoverflow.com/questions/3154454/what-is-the-most-efficient-way-to-calculate-the-least-common-multiple-of-two-int

Published:
01 July 2010

Accessed:
03 August 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment