Open this example in the interactive library
Source code
const gcd = (a, b) => {
while (b) [a, b] = [b, a % b];
return Math.abs(a);
};
console.log(gcd(48, 18));
Sample output
6

Compute greatest common divisor. A self-contained example using the JavaScript runtime.
Open this example in the interactive library
const gcd = (a, b) => {
while (b) [a, b] = [b, a % b];
return Math.abs(a);
};
console.log(gcd(48, 18));
6