Update lcm.md
This commit is contained in:
@ -1,16 +1,15 @@
|
|||||||
### lcm
|
### 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.
|
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
|
||||||
The GCD formula uses recursion.
|
The GCD formula uses recursion.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const lcm = (...arr) => {
|
const lcm = (...arr) => {
|
||||||
let data = [].concat(...arr);
|
|
||||||
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||||
const helperLcm = (x, y) => x * y / gcd(x, y);
|
const _lcm = (x, y) => x * y / gcd(x, y);
|
||||||
return arr.reduce((a, b) => helperLcm(a, b));
|
return [].concat(...arr).reduce((a, b) => _lcm(a, b));
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user