From ae2b5c2b6e2509e8c11c9bbb1bdb8eabcb8d0991 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 29 Dec 2017 16:34:06 +0530 Subject: [PATCH 1/5] update lcm and gcd --- snippets/arrayGcd.md | 17 ----------------- snippets/arrayLcm.md | 18 ------------------ snippets/gcd.md | 10 +++++++--- snippets/lcm.md | 13 ++++++++----- 4 files changed, 15 insertions(+), 43 deletions(-) delete mode 100644 snippets/arrayGcd.md delete mode 100644 snippets/arrayLcm.md diff --git a/snippets/arrayGcd.md b/snippets/arrayGcd.md deleted file mode 100644 index feb20cc00..000000000 --- a/snippets/arrayGcd.md +++ /dev/null @@ -1,17 +0,0 @@ -### arrayGcd - -Calculates the greatest common denominator (gcd) of an array of numbers. - -Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the greatest common denominator of an array of numbers. - -```js -const arrayGcd = arr => { - const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - return arr.reduce((a, b) => gcd(a, b)); -}; -``` - -```js -arrayGcd([1, 2, 3, 4, 5]); // 1 -arrayGcd([4, 8, 12]); // 4 -``` diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md deleted file mode 100644 index 5486edea5..000000000 --- a/snippets/arrayLcm.md +++ /dev/null @@ -1,18 +0,0 @@ -### arrayLcm - -Calculates the lowest common multiple (lcm) of an array of numbers. - -Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the lowest common multiple of an array of numbers. - -```js -const arrayLcm = arr => { - const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - const lcm = (x, y) => x * y / gcd(x, y); - return arr.reduce((a, b) => lcm(a, b)); -}; -``` - -```js -arrayLcm([1, 2, 3, 4, 5]); // 60 -arrayLcm([4, 8, 12]); // 24 -``` diff --git a/snippets/gcd.md b/snippets/gcd.md index e651c30db..4253e5fc3 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -1,13 +1,17 @@ ### gcd -Calculates the greatest common divisor between two numbers. +Calculates the greatest common divisor between two or more numbers/arrays. -Use recursion. +The helperGcd function uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js -const gcd = (x, y) => (!y ? x : gcd(y, x % y)); +const gcm = (...arr) => { + arr = [].concat(...arr) +const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); +return arr.reduce((a, b) => helperGcd(a, b)) +} ``` ```js diff --git a/snippets/lcm.md b/snippets/lcm.md index 9152f9aac..02d984656 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -1,17 +1,20 @@ ### lcm -Returns the least common multiple of two numbers. +Returns the least common multiple of two or numbers/arrays. 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); -}; +const lcm = (...arr) => { + arr = [].concat(...arr) +const gcd = (x, y) => (!y ? x : gcd(y, x % y)); +const helperLcm = (x, y) => x * y / gcd(x, y); +return arr.reduce((a, b) => helperLcm(a, b)) +} ``` ```js lcm(12, 7); // 84 +lcm([1,3,4],5); // 60 ``` From 71019b719747f4aaf9d3c46701e552d736484331 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Fri, 29 Dec 2017 16:40:42 +0530 Subject: [PATCH 2/5] Update gcd.md --- snippets/gcd.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/gcd.md b/snippets/gcd.md index 4253e5fc3..251fc14ae 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -8,9 +8,9 @@ Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js const gcm = (...arr) => { - arr = [].concat(...arr) +let data = [].concat(...arr) const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); -return arr.reduce((a, b) => helperGcd(a, b)) +return data.reduce((a, b) => helperGcd(a, b)) } ``` From ae8749df271441cdcb4715c75bd6998228e7eabf Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Fri, 29 Dec 2017 16:41:22 +0530 Subject: [PATCH 3/5] Update lcm.md --- snippets/lcm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/lcm.md b/snippets/lcm.md index 02d984656..9f8085d61 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -7,7 +7,7 @@ The GCD formula uses recursion. ```js const lcm = (...arr) => { - arr = [].concat(...arr) +let data = [].concat(...arr) const gcd = (x, y) => (!y ? x : gcd(y, x % y)); const helperLcm = (x, y) => x * y / gcd(x, y); return arr.reduce((a, b) => helperLcm(a, b)) From d74df3dc8fbb40bbee93aa14c5ed58dc5c120f69 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 29 Dec 2017 13:11:38 +0200 Subject: [PATCH 4/5] Update gcd.md --- snippets/gcd.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/gcd.md b/snippets/gcd.md index 251fc14ae..8c4011b4d 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -2,15 +2,15 @@ Calculates the greatest common divisor between two or more numbers/arrays. -The helperGcd function uses recursion. +The `helperGcd `function uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js -const gcm = (...arr) => { -let data = [].concat(...arr) -const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); -return data.reduce((a, b) => helperGcd(a, b)) +const gcd = (...arr) => { + let data = [].concat(...arr); + const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); + return data.reduce((a, b) => helperGcd(a, b)); } ``` From 19c0626b1321046740f7c0227b034e0c3b9e7156 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 29 Dec 2017 13:12:01 +0200 Subject: [PATCH 5/5] Update lcm.md --- snippets/lcm.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/lcm.md b/snippets/lcm.md index 9f8085d61..215158c30 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -7,10 +7,10 @@ The GCD formula uses recursion. ```js const lcm = (...arr) => { -let data = [].concat(...arr) -const gcd = (x, y) => (!y ? x : gcd(y, x % y)); -const helperLcm = (x, y) => x * y / gcd(x, y); -return arr.reduce((a, b) => helperLcm(a, b)) + let data = [].concat(...arr); + const gcd = (x, y) => (!y ? x : gcd(y, x % y)); + const helperLcm = (x, y) => x * y / gcd(x, y); + return arr.reduce((a, b) => helperLcm(a, b)) } ```