Update lcm.md

This commit is contained in:
atomiks
2017-12-30 01:54:53 +11:00
committed by GitHub
parent 9f8b72fa64
commit d70d11deac

View File

@ -1,16 +1,15 @@
### lcm
Returns the least common multiple of two or numbers/arrays.
Returns the least common multiple of two or more numbers/arrays.
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
The GCD formula uses recursion.
```js
const lcm = (...arr) => {
let data = [].concat(...arr);
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
const helperLcm = (x, y) => x * y / gcd(x, y);
return arr.reduce((a, b) => helperLcm(a, b));
const _lcm = (x, y) => x * y / gcd(x, y);
return [].concat(...arr).reduce((a, b) => _lcm(a, b));
};
```