Computing the GCD via factoring is not very practical for larger numbers, because factoring takes a long time. It turns out there is a much better way that is actually used in practice. To see why it works, we’ll first need a few lemmas and theorems about \gcd .
This seems a bit tedious—and indeed, we can do slightly better. Notice that we end up repeatedly subtracting b from a: \gcd (a,b) = \gcd (a-b,b) = \gcd (a-2b,b) = \gcd (a-3b,b) = \dots And we keep doing this until a is finally smaller than b. But that's exactly what \mathbin {\mathbf {mod}} does! This leads to the following observation:
Notice that by the division algorithm, a \mathbin {\mathbf {mod}} b is guaranteed to be strictly smaller than b. So if we replace \gcd (a,b) with \gcd (a \mathbin {\mathbf {mod}} b, b), the very next thing we would do is to switch the two arguments; hence, we might as well combine these two steps into one. This finally leads to the most elegant, modern formulation of the algorithm.