Merge pull request #197 from Pl4gue/patch-1
Create least-common-multiple-(LCM).md
This commit is contained in:
12
snippets/least-common-multiple-(LCM).md
Normal file
12
snippets/least-common-multiple-(LCM).md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
### Least common multiple (LCM)
|
||||||
|
|
||||||
|
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
|
||||||
|
The GCD formula uses recursion.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const lcm = (x,y) => {
|
||||||
|
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||||
|
return Math.abs(x*y)/(gcd(x,y));
|
||||||
|
};
|
||||||
|
// lcm(12,7) -> 84
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user