diff --git a/snippets/least-common-multiple-(LCM).md b/snippets/least-common-multiple-(LCM).md index 1696114f2..4f3fc2822 100644 --- a/snippets/least-common-multiple-(LCM).md +++ b/snippets/least-common-multiple-(LCM).md @@ -1,11 +1,12 @@ ### Least common multiple (LCM) -Use this lcm formula `lcm(a,b)=|a*b|/gcd(a,b)` for calculating the least common multiple of two numbers. -Makes use of the [GCD snippet](https://github.com/Chalarangelo/30-seconds-of-code#greatest-common-divisor-gcd). +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) => Math.abs(x*y)/(gcd(x,y)); -const gcd = (x, y) => !y ? x : gcd(y, x % y); -// lcm(10,5) -> 10 +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 ```