/** * Computes and returns the greatest common divisor of two integers `a` and `b`, * using the [Euclidean Algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm). * * @doc */ function bGcdInt(a: bigint, b: bigint): bigint { a = a < 0n ? -a : a; b = b < 0n ? -b : b; // The below 2 commented lines represent Euclid's original algorithm. //if (a === b) { return a; } //return a > b ? gcdInt(a - b, b) : gcdInt(a, b - a); if (a === 0n) { return b; } if (b === 0n) { return a; } while (b !== 0n) { const t = b; b = a % b; a = t; } return a; } /** * Naively computes and returns the greatest common divisor of 2 or more * integers by taking each integer in turn and calculating the GCD of that * integer and the previously calculated GCD (where the first GCD is simply * taken as the first number). * * @param vals the integers for which the GCD is to be calculated * * @doc */ function bGcdInts(vals: bigint[]): bigint { const vals_ = vals.slice(); const len = vals_.length; // make array of numbers all positive for (let i=0; i 1) { const newVals = []; const len = vals_.length; for (const i=0; i