Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Greatest Common Divisor by mortonfox
// jshint esversion: 6
import assert from "assert";
function gcd(a, b) {
return b ? gcd(b, a % b) : a;
}
function greatestCommonDivisor(...args: number[]): number {
return args.reduce((g, v) => gcd(g, v));
}
console.log('Example:');
console.log(greatestCommonDivisor(6, 4));
// These "asserts" are used for self-checking
assert.equal(greatestCommonDivisor(6, 4), 2);
assert.equal(greatestCommonDivisor(2, 4, 8), 2);
assert.equal(greatestCommonDivisor(2, 3, 5, 7, 11), 1);
assert.equal(greatestCommonDivisor(3, 9, 3, 9), 3);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
Nov. 20, 2020
Comments: