diff --git a/snippets/least-common-multiple-(LCM).md b/snippets/least-common-multiple-(LCM).md new file mode 100644 index 000000000..4f3fc2822 --- /dev/null +++ b/snippets/least-common-multiple-(LCM).md @@ -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 +```