Skip to main content

Euclid Would Be Proud: GCD in a JavaScript One-Liner

·43 words·1 min · Download pdf

By 1950, the word algorithm was mostly associated with “Euclid’s Algorithm". -Knuth

This algorithm for finding Greatest Common Divisor, one of the oldest, fits in a JavaScript one liner.

function gcd(a,b) { return b ? gcd(b, a % b) : a; }

Discussion