Table of contents
Open Table of contents
Visual Intuition
The idea behind Euclid’s algorithm is simple: if you want the GCD of two numbers, you can repeatedly replace the larger number with the remainder of dividing it by the smaller, until the remainder is zero. The last non-zero remainder is the GCD.
Here is Algorithm E applied to and :
The left side shows the computation trace for . The right side shows the algorithm’s flow: divide, check if the remainder is zero, and loop until it is.
Knuth’s Formulation
Knuth defines Algorithm E with three steps:
E1. [Find remainder.] Divide by and let be the remainder. (We will have .)
E2. [Is it zero?] If , the algorithm terminates; is the answer.
E3. [Reduce.] Set , , and go back to step E1.
In mathematical notation, this computes:
by repeatedly applying the identity until the second argument reaches zero.
The correctness relies on a key property: if , then any common divisor of and is also a divisor of , and vice versa. So .
Relation to algorithm definition
Knuth maps Euclid’s algorithm onto this framework explicitly. For :
- = all pairs of non-negative integers plus intermediate states
- = pairs with
- = states where the remainder is zero (the answer is ready)
- = one step of “divide, check remainder, reduce”
Complexity and Notes
Time complexity: — Gabriel Lame proved in 1844 that the number of steps is at most five times the number of digits in the smaller number. This was one of the first analyses of an algorithm’s running time.