Update lcm.md
This commit is contained in:
@ -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));
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user