Skip to content

Instantly share code, notes, and snippets.

@ApsarasX
Created August 30, 2017 12:56
Show Gist options
  • Save ApsarasX/270a6eea0e4905aed7a3385982321d66 to your computer and use it in GitHub Desktop.
Save ApsarasX/270a6eea0e4905aed7a3385982321d66 to your computer and use it in GitHub Desktop.
about gcd and lcm(关于最大公约数和最小公倍数)
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