The greatest common divisor (GCD) of two integers $a$ and $b$ is defined to be the largest integer that divides both $a$ and $b$ with no remainder. For example, the GCD of 16 and 28 is 4. In chapter 2, when we investigate how to implement rational-number arithmetic, we will need to be able to compute GCDs in order to reduce rational numbers to lowest terms. (To reduce a rational number to lowest terms, we must divide both the numerator and the denominator by their GCD. For example, 16/28 reduces to 4/7.) One way to find the GCD of two integers is to factor them and search for common factors, but there is a famous algorithm that is much more efficient.
The idea of the algorithm is based on the observation that, if $r$ is the remainder when $a$ is divided by $b$, then the common divisors of $a$ and $b$ are precisely the same as the common divisors of $b$ and $r$. Thus, we can use the equation \[ \textrm{GCD} (a, b) = \textrm{GCD}(b, r) \] to successively reduce the problem of computing a GCD to the problem of computing the GCD of smaller and smaller pairs of integers. For example, \begin{eqnarray*} \textrm{GCD}(206,40) & = & \textrm{GCD}(40,6) \\ & = & \textrm{GCD}(6,4) \\ & = & \textrm{GCD}(4,2) \\ & = & \textrm{GCD}(2,0) \\ & = & 2 \end{eqnarray*} reduces $\textrm{GCD}(206, 40)$ to $\textrm{GCD}(2, 0)$, which is 2. It is possible to show that starting with any two positive integers and performing repeated reductions will always eventually produce a pair where the second number is 0. Then the GCD is the other number in the pair. This method for computing the GCD is known as Euclid's Algorithm.[1]
It is easy to express Euclid's Algorithm as a function:
function gcd(a, b) { return b === 0 ? a : gcd(b, a % b); }
This generates an iterative process, whose number of steps grows as the logarithm of the numbers involved.
The fact that the number of steps required by Euclid's Algorithm has logarithmic growth bears an interesting relation to the Fibonacci numbers:
Lamé's Theorem: If Euclid's Algorithm requires $k$ steps to compute the GCD of some pair, then the smaller number in the pair must be greater than or equal to the $k$th Fibonacci number.[2]
We can use this theorem to get an order-of-growth estimate for Euclid's Algorithm. Let $n$ be the smaller of the two inputs to the function. If the process takes $k$ steps, then we must have $n\geq {\textrm{Fib}} (k)\approx\phi^k/\sqrt{5}$. Therefore the number of steps $k$ grows as the logarithm (to the base $\phi$) of $n$. Hence, the order of growth is $\Theta(\log n)$.
gcd(206, 40) 40 === 0 ? 206 : gcd(40, 206 % 40) gcd(40, 206 % 40) 206 % 40 === 0 ? 40 : gcd(206 % 40, 40 % (206 % 40)) // remainder operation (1) 6 === 0 ? 40 : gcd(206 % 40, 40 % (206 % 40)) gcd(206 % 40, 40 % (206 % 40)) 40 % (206 % 40) === 0 ? 206 % 40 : gcd(40 % (206 % 40), (206 % 40) % (40 % (206 % 40))) // remainder operations (2) and (3) 4 === 0 ? 206 % 40 : gcd(40 % (206 % 40), (206 % 40) % (40 % (206 % 40))) gcd(40 % (206 % 40), (206 % 40) % (40 % (206 % 40))) (206 % 40) % (40 % (206 % 40)) === 0 ? 40 % (206 % 40) : gcd((206 % 40) % (40 % (206 % 40)), (40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40))) // remainder operations (4), (5), (6), (7) 2 === 0 ? 40 % (206 % 40) : gcd((206 % 40) % (40 % (206 % 40)), (40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40)))) gcd((206 % 40) % (40 % (206 % 40)), (40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40))) (40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40))) === 0 ? (206 % 40) % (40 % (206 % 40)) : gcd((40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40)), ((206 % 40) % (40 % (206 % 40))) % ((40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40)))) // remainder operations (8), (9), (10), (11), (12), (13), (14) 0 === 0 ? (206 % 40) % (40 % (206 % 40)) : gcd((40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40)), ((206 % 40) % (40 % (206 % 40))) % ((40 % (206 % 40)) % ((206 % 40) % (40 % (206 % 40)))) (206 % 40) % (40 % (206 % 40)) // remainder operations (15), (16), (17), (18) 2
gcd(206, 40) 40 === 0 ? 206 : gcd(40, 206 % 40) gcd(40, 206 % 40) // remainder operation (1) gcd(40, 6) 6 === 0 ? 40 : gcd(6, 40 % 6) gcd(6, 40 % 6) // remainder operation (2) gcd(6, 4) 4 === 0 ? 6 : gcd(4, 6 % 4) gcd(4, 6 % 4) // remainder operation (3) gcd(4, 2) 2 === 0 ? 4 : gcd(2, 4 % 2) gcd(2, 4 % 2) // remainder operation (4) gcd(2, 0) 0 === 0 ? 2 : gcd(0, 2 % 0) 2