Last active
August 3, 2016 10:50
-
-
Save HopefulLlama/94e768d482806ae681fa606e8d9b026f to your computer and use it in GitHub Desktop.
Finds the Least Common Multiple for two numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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