diff --git a/algorithms/math/gcd.js b/algorithms/math/gcd.js index f7f4889..96b86a8 100644 --- a/algorithms/math/gcd.js +++ b/algorithms/math/gcd.js @@ -1,13 +1,27 @@ -// 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, 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 +*/ +const gcd = (a, b) => { + while (b != 0) + [a, b] = [b, a % b]; + return abs(a) } + +const tuples = [[6, 9], [6, 12], [-12, -18], [7, 14], [7, 13], [1/2, 2]]; + +for (const [a, b] of tuples) + console.log(`GCD of ${a} and ${b} is ${gcd(a, b)}`);