locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
24 lines (23 loc) • 767 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.gcd = gcd;
function gcd(a, b) {
// discuss at: https://locutus.io/python/gcd/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Returns the greatest common divisor of a and b
// example 1: gcd(48, 18)
// returns 1: 6
// example 2: gcd(0, 5)
// returns 2: 5
// example 3: gcd(7, 0)
// returns 3: 7
let left = Math.abs(Number.parseInt(String(a), 10));
let right = Math.abs(Number.parseInt(String(b), 10));
while (right !== 0) {
const temp = right;
right = left % right;
left = temp;
}
return left;
}