At each recursive step, gcd
will cut one of the arguments in half (at most). To see this, look at these two cases:
If b >= a/2
then on the next step you'll have a' = b
and b' < a/2
since the %
operation will remove b
or more from a
.
If b < a/2
then on the next step you'll have a' = b
and b' < a/2
since the %
operation can return at most b - 1
.
So on each recursive step, gcd
will cut one of the arguments in half (at most). This is O(log(N)) steps where N is the max of the initial a
and b
.