Created
August 30, 2017 12:56
-
-
Save ApsarasX/270a6eea0e4905aed7a3385982321d66 to your computer and use it in GitHub Desktop.
about gcd and lcm(关于最大公约数和最小公倍数)
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 gcd(x, y) { | |
while (y) { | |
[x, y] = [y, x % y]; | |
} | |
return x; | |
} | |
const lcm = (x,y) => x * y / gcd(x,y); | |
function multipleLCM(...nums) { | |
let [min, max] = (nums.length===1?nums[0]:nums).sort((a, b) => a - b); | |
let arr = Array.from(new Array(max - min + 1),(val, index) => index + min); | |
return arr.reduce((prev,cur) => lcm(prev, cur)); | |
} | |
//demo | |
multipleLCM(1,5); //60 | |
multipleLCM([1,5]); //60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment