GCD and the Euclidean Algorithm
Introduction
The greatest common divisor is one of the oldest and most fundamental concepts in mathematics. When we reduce a fraction to lowest terms, we divide both numerator and denominator by their greatest common divisor. When we solve Diophantine equations, we need to know whether a solution exists—and the GCD tells us. When we implement the RSA cryptosystem, the Euclidean algorithm computes the modular inverses that make encryption possible.
The Euclidean algorithm, described by Euclid around BCE, is arguably the oldest non-trivial algorithm still in widespread use today. Its elegance lies in reducing a seemingly difficult problem—finding the largest common factor of two numbers—to a sequence of simple divisions. The algorithm is fast, easy to implement, and reveals deep structural properties of the integers.
Understanding the GCD and Euclidean algorithm is essential for number theory, abstract algebra, cryptography, and computer science. These concepts form the foundation upon which modular arithmetic, prime factorization theory, and much of modern cryptography are built.
Definition of the Greatest Common Divisor
Basic Definition
Let and be integers, not both zero. A common divisor of and is an integer such that and (that is, divides both and ).
The greatest common divisor of and , denoted or simply , is the largest positive integer that divides both and .
By convention, for any nonzero integer , since every integer divides . The expression is typically left undefined or set to by convention.
Examples
The divisors of are . The divisors of are . The common divisors are , so .
For : The divisors of are and the divisors of are . The greatest common divisor is .
Coprime Integers
Two integers and are called coprime (or relatively prime) if . This means they share no common factor other than .
For example, since and share no prime factors. Coprimality is central to modular arithmetic: an integer has a multiplicative inverse modulo if and only if .
Properties of the GCD
The greatest common divisor satisfies several fundamental properties that make it amenable to computation and theoretical analysis.
Basic Properties
For any integers , , and :
Commutativity:
Associativity:
Absolute values:
Idempotence:
Identity:
The Key Divisibility Property
The property that makes the Euclidean algorithm work is:
for any integer
In particular, if (division with remainder), then:
This is because any common divisor of and must also divide and conversely, any common divisor of and must divide Thus the set of common divisors is the same, and so is the greatest one.
GCD and Prime Factorization
If we know the prime factorizations of and we can compute the GCD by taking the minimum exponent for each prime:
If and then:
For example, and , so
.
While this formula is conceptually clear, factoring large integers is computationally hard. The Euclidean algorithm provides a much faster way to compute the GCD without factoring.
The Euclidean Algorithm
The Division Algorithm
The Euclidean algorithm relies on the division algorithm, which states: for any integers and with there exist unique integers (quotient) and (remainder) such that:
We write for the remainder.
Algorithm Description
To compute with repeatedly apply the key property:
The sequence of remainders is strictly decreasing and bounded below by zero, so it must eventually reach zero. The last nonzero remainder is the GCD.
Bezouts Identity
One of the most important theoretical results about the GCD is Bezouts identity:
For any integers and (not both zero), there exist integers and such that:
The integers and are called Bezout coefficients. They are not unique: if is one solution, then is also a solution for any integer where
Bezouts identity has a powerful corollary: is the smallest positive integer that can be expressed as a linear combination with integer coefficients.
The Extended Euclidean Algorithm
The extended Euclidean algorithm computes not only but also the Bezout coefficients and The idea is to track how each remainder can be expressed as a linear combination of and
We maintain the invariant that at each step we have . Starting with:
At each step, if then so:
Applications
Reducing Fractions
To reduce a fraction to lowest terms, divide both numerator and denominator by
The resulting fraction is in lowest terms because the new numerator and denominator are coprime.
Modular Multiplicative Inverse
In modular arithmetic, we often need to find the multiplicative inverse of modulo This inverse exists if and only if
By Bezouts identity, if there exist and with Taking this modulo
So is the modular inverse. The extended Euclidean algorithm computes it efficiently.
Solving Linear Diophantine Equations
A linear Diophantine equation has integer solutions if and only if
If divides use the extended Euclidean algorithm to find with then multiply by to get a particular solution. The general solution is:
for any integer
Cryptography: RSA
In RSA encryption, we choose two large primes and compute and select an encryption exponent with and compute the decryption exponent using the extended Euclidean algorithm.
The security of RSA relies on the difficulty of factoring but the extended Euclidean algorithm makes key generation efficient.
Algorithm Complexity
The Euclidean algorithm is remarkably efficient. How many steps does it take to compute
Lames Theorem
The number of steps in the Euclidean algorithm never exceeds times the number of digits in the smaller input.
More precisely, if the algorithm takes steps, then where is the th Fibonacci number. Since where is the golden ratio, the number of steps is
The worst case occurs when and are consecutive Fibonacci numbers. For example, requires exactly divisions, each with quotient
Bit Complexity
If and have at most bits, the Euclidean algorithm runs in divisions. Each division of bit numbers takes bit operations with schoolbook arithmetic, giving a total of bit operations. More sophisticated analysis shows the algorithm actually runs in bit operations.
Relationship with Least Common Multiple
The least common multiple is the smallest positive integer divisible by both and The GCD and LCM are related by a beautiful formula:
This can be understood through prime factorizations: if the GCD takes the minimum exponent for each prime, the LCM takes the maximum. For any prime with exponents in and in
This allows us to compute the LCM efficiently using the GCD:
Generalizations
The GCD extends naturally to more than two integers:
The concept also generalizes to other algebraic structures. In polynomial rings, we can define the GCD of polynomials and compute it using the polynomial version of the Euclidean algorithm. In abstract algebra, the GCD is generalized to principal ideal domains.
Connection to Other Concepts
The GCD and Euclidean algorithm connect to many areas of mathematics:
In continued fractions, the quotients from the Euclidean algorithm give the continued fraction expansion of
Eulers totient function counts integers from to that are coprime to It is fundamental to Eulers theorem and RSA.
The Chinese Remainder Theorem provides a way to solve systems of congruences, and its proof uses the extended Euclidean algorithm to construct solutions.
In abstract algebra, the integers form a Euclidean domain, and the Euclidean algorithm provides the computational foundation for the theory of principal ideal domains.
Summary
The greatest common divisor is the largest positive integer dividing both and Two integers are coprime when their GCD is
The Euclidean algorithm computes the GCD through repeated division: It terminates when the remainder is zero, and the last nonzero remainder is the GCD. The algorithm is efficient, running in steps.
Bezouts identity states that can always be expressed as for some integers and The extended Euclidean algorithm computes these coefficients alongside the GCD.
Applications include reducing fractions to lowest terms, computing modular inverses, solving linear Diophantine equations, and implementing cryptographic systems like RSA. The GCD is also related to the LCM by
The Euclidean algorithm is one of the oldest algorithms in mathematics, yet it remains fundamental to modern number theory, algebra, and computer science. Its elegance lies in reducing a complex problem to a sequence of simple operations, a principle that echoes throughout mathematics.