You can write the Euclidean algorithm recursively: gcd(a, b) = gcd(b, a mod b) if b ≠ 0, else a.
function gcd(a, b):
if b == 0:
return a
return gcd(b, a mod b)
This is shorter but uses call stack space. Both versions run in O(log min(a, b)) time.