From dcf0ba3ea2a94bddead9dcd2175d323e8f78994d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 17 Dec 2017 11:26:53 +0200 Subject: [PATCH] Update least-common-multiple-(LCM).md --- snippets/least-common-multiple-(LCM).md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 ```