Merge pull request #392 from Chalarangelo/fix-lcm

[FIX] lcm.md
This commit is contained in:
Angelos Chalaris
2017-12-29 16:56:33 +02:00
committed by GitHub

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));
};
```