Overview
The Euclidean algorithm transforms a common-divisor problem into a sequence of smaller problems. It is both an exact computational procedure and a short proof that greatest common divisors are accessible without knowing prime factorizations. Keeping track of the same divisions also produces coefficients for a linear combination of the original integers, which is the extended algorithm.
Prerequisites
Understand integer divisibility and division with remainder. For , division writes with . A greatest common divisor is chosen nonnegative, so negative starting inputs can first be replaced by their absolute values.
Definition and conventions
Start with a pair of nonnegative integers. While , replace it by , where is the remainder of on division by . Stop when the second component is zero. The first component of the final pair is .
For , the procedure stops immediately and returns zero, matching the convention for that gcd. Division by a zero second component is never attempted. If the initial first component is smaller, the first quotient is zero; the next pair simply reverses the order.
Intuition
The equality shows that a common divisor of is also a divisor of . The converse follows from . Therefore each step preserves exactly the same set of common divisors. Meanwhile the positive second components strictly decrease, so the process cannot continue forever.
Examples and non-examples
For and , the divisions are
The gcd is . The first equation gives the certificate . Hence has an integer solution, but does not.
For and , division yields remainders and . Back substitution gives , so is an inverse of modulo . It represents the same residue class as . Returning the last quotient instead of the last nonzero remainder would give an incorrect algorithm.
Important properties
The invariant and termination prove correctness. At the final pair , the nonnegative greatest common divisor is . Since the earlier pairs have the same common divisors, this is also the gcd of the initial pair.
Every remainder is an integer linear combination of the original inputs: this is true initially, and subtraction of a multiple preserves it. Thus back substitution constructs Bézout coefficients. Those coefficients are usually not unique. If and both inputs are nonzero, replacing by leaves the equality unchanged for any integer .
Connections and further reading
The procedure solves linear Diophantine equations and computes modular inverses when the gcd is one. Polynomial division provides an analogous algorithm over a field; general rings need a suitable division function. Judson, Subsection 2.2.1 gives the integer foundation.