From c7e5aab54625314bcba8c840044c6f75fcfd289d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Fri, 16 Sep 2022 04:14:36 -0400 Subject: [PATCH 1/2] Update gcd.js Strict mode, better docs, refactoring, etc... --- algorithms/math/gcd.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/algorithms/math/gcd.js b/algorithms/math/gcd.js index f7f4889..92db9ec 100644 --- a/algorithms/math/gcd.js +++ b/algorithms/math/gcd.js @@ -1,13 +1,24 @@ -// GCD - greatest common divisor or HCF - highest common factor +"use strict"; -function gcd (small, large) { - if (small === 0) { return large } else { return gcd(large % small, small) } -} +/** @typedef {(number|bigint)} numeric */ -const gcdList = [[6, 9], [6, 12], [12, 18], [7, 14], [7, 13]] +/** @param {numeric} x */ +const abs = x => x < 0 ? -x : x; -for (const set of gcdList) { - const small = set[0] - const large = set[1] - console.log(`GCD for ${small} and ${large} is ${gcd(small, large)}`) +/** + * GCD - greatest common divisor or HCF - highest common factor. + * Using the Euclidean algorithm (not to be confused with Euclid's algo). + * @param {numeric} a + * @param {numeric} b + * returns correct results even for non-integers (sometimes) +*/ +const gcd = (a, b) => { + while (b != 0) + [a, b] = [b, a % b]; + return abs(a) } + +const gcdList = [[6, 9], [6, 12], [12, 18], [7, 14], [7, 13]]; + +for (const [small, large] of gcdList) + console.log(`GCD for ${small} and ${large} is ${gcd(small, large)}`); From 32ad85d6720b2c8bb29cd11aa2ba46b09d87b083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Sat, 17 Sep 2022 11:18:52 -0400 Subject: [PATCH 2/2] Update `gcd.js` --- algorithms/math/gcd.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/algorithms/math/gcd.js b/algorithms/math/gcd.js index 92db9ec..96b86a8 100644 --- a/algorithms/math/gcd.js +++ b/algorithms/math/gcd.js @@ -1,24 +1,27 @@ "use strict"; -/** @typedef {(number|bigint)} numeric */ +/** @typedef {number|bigint} numeric */ /** @param {numeric} x */ const abs = x => x < 0 ? -x : x; /** * GCD - greatest common divisor or HCF - highest common factor. - * Using the Euclidean algorithm (not to be confused with Euclid's algo). + * Using the Euclidean algorithm (not to be confused with Euclid's algo, which does subtraction). + * + * Argument order doesn't matter. + * `return`s correct results even for non-integers (sometimes, because of rounding errors). + * This fn is 2-adic, the variadic implementation is left as an exercise to the reader. * @param {numeric} a * @param {numeric} b - * returns correct results even for non-integers (sometimes) */ const gcd = (a, b) => { - while (b != 0) - [a, b] = [b, a % b]; - return abs(a) + while (b != 0) + [a, b] = [b, a % b]; + return abs(a) } -const gcdList = [[6, 9], [6, 12], [12, 18], [7, 14], [7, 13]]; +const tuples = [[6, 9], [6, 12], [-12, -18], [7, 14], [7, 13], [1/2, 2]]; -for (const [small, large] of gcdList) - console.log(`GCD for ${small} and ${large} is ${gcd(small, large)}`); +for (const [a, b] of tuples) + console.log(`GCD of ${a} and ${b} is ${gcd(a, b)}`);