The Euclidean Algorithm
How do you find the largest number that divides both 1071 and 462? You must do this without factoring either one. Factoring large numbers is slow. For numbers with hundreds of digits, it is computationally infeasible even for modern computers. Yet the question of a common divisor is fundamental to nearly all of number theory. It matters for simplifying fractions and for building and breaking cryptographic systems. The Euclidean Algorithm answers this question with breathtaking efficiency. It uses nothing more than repeated division with remainder. The idea was recorded by Euclid around 300 BCE. It appears in Book VII of the Elements. It is one of the oldest algorithms in continuous use. It replaces a hard problem with a short sequence of easy problems. That hard problem is finding common divisors of two possibly huge numbers. Each easy problem is a division with a smaller remainder than the last. The remainders shrink so fast that the whole process terminates quickly. The last nonzero remainder is provably the greatest common divisor. The big picture is this: divisibility questions about a pair of integers can be reduced. They reduce, without loss of information, to divisibility questions about a strictly smaller pair. This reduction step is reversible. That reversibility is exactly what lets us compute the gcd. It also lets us later express it as an integer combination of the original two numbers. This happens in the Extended Euclidean Algorithm. It is the seed of Bezout's Identity. This article gives the precise algorithm. It proves the algorithm terminates in finitely many steps. It also proves rigorously that its output equals . It relies only on Divisibility and the Division Algorithm and the definition of The Greatest Common Divisor. Both of these are assumed known. It sets up the machinery needed for The Extended Euclidean Algorithm.
- The Euclidean Algorithm computes using only repeated division with remainder — no factoring required.
- Its core fact: whenever , for any valid quotient-remainder pair.
- Applying this repeatedly produces a strictly decreasing sequence of nonnegative remainders, forcing termination.
- The last nonzero remainder in the chain is exactly .
- Its running time is polynomial in the number of digits, and worst-case behavior is governed by Fibonacci numbers.
- Key idea: replace by over and over; the gcd never changes, and the numbers shrink until one hits zero.
Where this lives today
The Euclidean Algorithm is the computational backbone of elementary number theory and remains indispensable in modern computing. In cryptography, the Extended Euclidean Algorithm — a light extension of this procedure that also tracks Bezout coefficients — is used to compute modular inverses in RSA key generation and in elliptic-curve cryptography. In computer algebra systems, it underlies polynomial gcd computation (via an analogous algorithm on polynomials over a field), which in turn is essential for simplifying rational functions and factoring. In simplifying fractions, the algorithm reduces to lowest terms by dividing numerator and denominator by . In the analysis of algorithms, it is the textbook example for proving termination via a strictly decreasing well-ordered quantity, and Lamé's theorem connecting its worst-case behavior to Fibonacci numbers is a classic result in complexity analysis. It also appears in continued fraction expansions: the sequence of quotients produced by the algorithm applied to and is exactly the continued fraction expansion of , linking gcd computation to Diophantine approximation.
Ask the tutor: How does tracking coefficients through the Euclidean Algorithm's steps produce the Bezout coefficients with ?
Let be integers, not both zero. Assume without loss of generality (if and , then by definition, and no division is needed). Define a sequence of pairs by setting
and, as long as , applying the Division Algorithm to write
for . The Euclidean Algorithm is the procedure of repeating this division step until some remainder is reached. Its output is the last nonzero remainder, (with the convention that if already, the output is ).
Intuition
Think of as a shared "unit of measurement" for two lengths and . If you lay the shorter length down along the longer length as many times as it fits, a leftover piece remains with . Any ruler that measures both and exactly must also measure this leftover piece exactly — and conversely, any ruler measuring and exactly also measures . So the pair and the pair have exactly the same common measuring-sticks, hence the same greatest one. This is the entire content of the algorithm: replace by the strictly smaller pair , and repeat. Since remainders are nonnegative integers that strictly decrease at every step, the process cannot continue forever — it must hit a remainder of , at which point the previous remainder is the answer. A second way to see it: picture the Euclidean algorithm as repeatedly subtracting the smaller number from the larger (division is just repeated subtraction done all at once). Geometrically, this is the ancient process of finding the largest square tile that exactly tiles an rectangle — lay down squares along the length , leave a strip of width , then tile that strip with squares of the next size down, and so on.
Let be integers with . Define the sequence , , and for each with , let be the unique integers given by the Division Algorithm satisfying
Then: 1. (Termination) There exists a smallest index such that ; that is, the sequence of divisions terminates after finitely many steps. 2. (Correctness) If (i.e. immediately), then . If , then
the last nonzero remainder produced by the algorithm.
Step 0: a key lemma about divisibility of remainders.
∎lemma[Invariance of gcd under division with remainder] If for integers with , then
::: Proof of Lemma. Let and . (Both exist: guarantees exists by definition of gcd; and since , exists as well, as alone is a nonzero common divisor candidate — more precisely, gcd of two integers not both zero always exists, and here .) We show 's defining set of common divisors matches, by showing the set of common divisors of equals the set of common divisors of . () Suppose and . Since , and divides both terms on the right (as , and ), it follows . So is a common divisor of and . () Suppose and . Since , and divides both terms on the right, it follows . So is a common divisor of and . Thus the common divisors of are exactly the common divisors of ; in particular these two sets have the same maximum element, so . (Lemma)
Step 1: Termination. Consider the sequence of remainders produced as long as they are defined (i.e. as long as the previous remainder is nonzero). By the Division Algorithm's conclusion applied at each step,
Hence, as long as no remainder in the sequence is zero, we get a strictly decreasing chain of nonnegative integers
(Here we use from the first division, and thereafter since for so .) A strictly decreasing sequence of nonnegative integers cannot be infinite: by the well-ordering principle, any nonempty set of nonnegative integers has a least element, and a strictly decreasing infinite sequence would produce a subset with no least element (take the sequence itself as the set — for any term, a later term is smaller), a contradiction. Therefore the sequence must reach after finitely many terms. Let be the first index with (if already, then ). This proves termination, establishing part 1.
Step 2: Correctness. We prove by induction on , for , the statement
Base case : asserts , which is immediate since by definition. Inductive step: Suppose holds for some , i.e. , and (guaranteed since implies the division defining took place, requiring ). By construction,
so applying the Lemma with gives
Combined with the inductive hypothesis , we conclude
which is exactly . This completes the induction, valid for all from to . In particular, gives
Case : Then directly gives trivially — but we want to identify this with . Since , by definition of gcd, (as every integer divides , so the common divisors of and are exactly the divisors of , whose greatest element is , assuming ). And says — to close the loop, note means was reached on the first division , and the Lemma applied directly gives . This matches the claimed output . Case : Since is the first zero remainder, we know (else would not be the last valid index with a nonzero remainder producing a further division — more precisely by minimality of as the first zero index, since is not zero as ). Now since , the same computation as above (Lemma applied to ) gives
using (indeed ) from the Division Algorithm. Combining with : . This proves , the last nonzero remainder, completing part 2 and the theorem.
Worked examples
Example 1: . We apply the algorithm, dividing the larger by the smaller at each stage and tracking quotient and remainder explicitly.
The last nonzero remainder is . By the theorem, . Check: and , and since is prime and does not divide . So indeed is the greatest common divisor — consistent with the algorithm's output.
Example 2: , illustrating a negative input. Since is typically defined via divisibility (which does not care about sign), we may take , , or equivalently work with since the set of common divisors of is unaffected by replacing an argument with its absolute value (any ). We proceed with , (ordering so the Division Algorithm applies cleanly with a positive divisor):
The last nonzero remainder is , so , and hence as well. Check: and , with (since is prime and ). This confirms is correct.
The theorem's proof crucially applies the Division Algorithm to , which requires (the Division Algorithm is only stated, and only true, for a nonzero divisor). Suppose we try to drop this hypothesis and "divide by zero" anyway — say naively attempt
when a remainder happens to be (as in Example 1's last line, , followed by an attempted next step ""). There is no valid quotient-remainder pair with , since no nonnegative integer is strictly less than . The Division Algorithm's conclusion is vacuous or undefined here — division by is simply not permitted. If one ignored this and, say, declared and continued the sequence indefinitely without ever stopping, termination would fail: the "sequence" neither decreases nor reaches a legitimate end, and the inductive argument for correctness (Step 2 of the proof) would break down entirely, since was derived precisely from a genuine application of the Lemma requiring a valid division. This shows why the algorithm's very definition insists on stopping exactly when a remainder hits , rather than trying to divide by that zero remainder.
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| , | No division performed; output is directly | by definition of gcd |
| is undefined (or by convention ) | Every integer divides , so there is no greatest divisor unless a convention is fixed | |
| Negative inputs | Replace by first, or note the algorithm still works since remainders are forced nonnegative by the Division Algorithm | Divisibility and gcd depend only on absolute values |
| One input divides the other exactly | Algorithm terminates in one step; output is | The remainder is immediately |
| Worst-case input size | Consecutive Fibonacci numbers | Each step produces quotient , the slowest possible decrease, giving steps — Lamé's theorem |
The mathematician behind it
Origins · 1 minEuclid of Alexandria and how this was discovered
Euclid is known almost entirely through his mathematical writing rather than biographical record; even his birthplace and dates are uncertain, and some historians have questioned whether "Euclid" designates a single author or a school. His Elements, a thirteen-book compilation and systematization of geometry and number theory, became the most reproduced mathematical text in history, shaping mathematical exposition — the very idea of building rigorously from definitions and axioms to theorems — for over two millennia. Propositions VII.1 and VII.2 of the Elements present the algorithm for finding the greatest common measure of two magnitudes, phrased geometrically via repeated subtraction, precisely the algorithm treated in this article.
Why does the Euclidean Algorithm always terminate?
Because each division step produces a remainder that is strictly smaller than the divisor used, and remainders are always nonnegative integers by the Division Algorithm. This creates a strictly decreasing sequence of nonnegative integers, . Such a sequence cannot decrease forever, since the well-ordering principle guarantees every nonempty set of nonnegative integers has a least element — an infinite strictly decreasing sequence would contradict this. Hence the sequence must reach after finitely many steps, at which point the algorithm halts. In fact the number of steps is at most roughly times the number of decimal digits of the smaller input, by Lamé's theorem, so termination is not just guaranteed but fast.
Does the order in which I feed the two numbers matter?
Not for correctness, only for the number of steps. If you start with where , the very first division simply swaps the roles, since and , and the algorithm proceeds identically to starting from . So feeding the smaller number first costs at most one extra trivial step but produces the same final gcd. Some implementations explicitly swap so that the larger number is always divided by the smaller, purely as a minor efficiency convention, not because the mathematics requires it.
What happens if I try to compute with this algorithm?
The algorithm as stated requires to perform even the first division, since the Division Algorithm needs a nonzero divisor. If both and , there is no division to perform and no well-defined "greatest" common divisor, because every integer divides , so the divisors of the pair have no maximum. Most texts either leave undefined or adopt the convention by fiat; the Euclidean Algorithm itself simply does not apply to this degenerate input and should be special-cased before running.
How is this related to the Extended Euclidean Algorithm and Bezout's Identity?
The Euclidean Algorithm computes only the numerical value of . The Extended Euclidean Algorithm runs the same sequence of divisions but additionally tracks, at each step, coefficients expressing each remainder as an integer combination . Since the final nonzero remainder equals , its tracked coefficients satisfy — this is precisely Bezout's Identity. The two algorithms share the identical division steps; the extended version simply carries extra bookkeeping alongside them.
Can this algorithm be used on polynomials, not just integers?
Yes. Over a field (such as the rationals or reals), polynomials support an analogous division algorithm with remainder of strictly smaller degree, and the same repeated-division procedure computes the gcd of two polynomials. The termination argument is identical in spirit, replacing "strictly decreasing nonnegative integer" with "strictly decreasing nonnegative degree." This polynomial Euclidean algorithm is a workhorse of computer algebra systems for simplifying rational expressions and factoring.
Is there a faster algorithm than this for computing gcd?
For arbitrary-size integers, the standard Euclidean Algorithm already runs in time polynomial in the number of digits — specifically division steps by Lamé's theorem — which is extremely fast in practice. Variants like the binary GCD algorithm (which replaces division by bit shifts and subtraction) can be faster in low-level hardware implementations by avoiding costly division operations, and more advanced algorithms (based on fast integer multiplication, e.g. the Schönhage–Strassen approach) achieve near-linear time for very large inputs, but the classical Euclidean Algorithm remains the standard teaching and reference algorithm due to its simplicity and provable correctness.
Why does the Lemma's proof work with arbitrary integer , not just prime divisors?
The proof shows that the entire set of common divisors of coincides with the entire set of common divisors of , using only the basic fact that divisibility is preserved under addition and integer multiples ( and implies and for any integer ). This works for any integer , positive or negative, prime or composite — the argument never needs unique factorization or primality. That is precisely why the Euclidean Algorithm can compute gcds without ever factoring the numbers involved, which is the whole point of its efficiency advantage over factorization-based methods.