Skip to content

Greatest Common Divisor and Least Common Multiple: The Building Blocks of Number Theory

The Greatest Common Divisor (GCD) and Least Common Multiple (LCM) are fundamental concepts that appear everywhere in mathematics and cryptography. Think of them as the mathematical tools that help us understand how numbers relate to each other – and they're absolutely essential for everything from simplifying fractions to generating cryptographic keys. Let's explore these elegant concepts that have been fascinating mathematicians for over 2000 years.

The Greatest Common Divisor: Finding Common Ground

Definition and Intuition

The Greatest Common Divisor of two integers aa and bb is the largest positive integer that divides both numbers evenly.

Notation: We write this as gcd(a,b)\gcd(a, b) or sometimes (a,b)(a, b).

Formal Definition: For integers aa and bb (not both zero):

gcd(a,b)=max{dZ+:da and db}\gcd(a, b) = \max\{d \in \mathbb{Z}^+ : d \mid a \text{ and } d \mid b\}

Examples:

  • gcd(12,18)=6\gcd(12, 18) = 6 because 66 is the largest number that divides both 1212 and 1818
  • gcd(17,19)=1\gcd(17, 19) = 1 because 1717 and 1919 are both prime
  • gcd(0,5)=5\gcd(0, 5) = 5 because every integer divides 00

Fundamental Properties

The GCD has several beautiful properties that make it incredibly useful:

Property 1 (Symmetry):

gcd(a,b)=gcd(b,a)\gcd(a, b) = \gcd(b, a)

Property 2 (Sign Independence):

gcd(a,b)=gcd(a,b)\gcd(a, b) = \gcd(|a|, |b|)

Property 3 (Zero Property):

gcd(a,0)=afor a0\gcd(a, 0) = |a| \quad \text{for } a \neq 0

Property 4 (Linear Combination):

gcd(a,b)=gcd(a+kb,b)for any integer k\gcd(a, b) = \gcd(a + kb, b) \quad \text{for any integer } k

Property 5 (Divisibility):

\gcd(a, b) = \gcd(a \bmod b, b)

Proof of Property 4: Let d=gcd(a,b)d = \gcd(a, b). Then dad \mid a and dbd \mid b, so d(a+kb)d \mid (a + kb). Conversely, if d(a+kb)d' \mid (a + kb) and dbd' \mid b, then dad' \mid a. Therefore, the common divisors of (a,b)(a, b) and (a+kb,b)(a + kb, b) are identical. \square

The Euclidean Algorithm: Ancient Efficiency

The Euclidean Algorithm, dating back to Euclid's Elements (circa 300 BCE), remains the most efficient method for computing GCDs.

Algorithm:

function gcd(a, b):
    while b ≠ 0:
        temp = b
        b = a mod b
        a = temp
    return |a|

Mathematical Foundation: The algorithm is based on the fundamental property:

\gcd(a, b) = \gcd(b, a \bmod b)

Why It Works: At each step, we replace the pair (a,b)(a, b) with (b, a \bmod b). Since a \bmod b < b, the second number decreases at each step, ensuring termination.

Complexity Analysis: The algorithm terminates in at most O(logmin(a,b))O(\log \min(a, b)) steps, making it remarkably efficient even for very large numbers.

Detailed Example: Computing gcd(252,105)\gcd(252, 105)

Let's trace through the algorithm step by step:

252=2×105+42gcd(252,105)=gcd(105,42)105=2×42+21gcd(105,42)=gcd(42,21)42=2×21+0gcd(42,21)=gcd(21,0)=21\begin{aligned} 252 &= 2 \times 105 + 42 & \gcd(252, 105) &= \gcd(105, 42) \\ 105 &= 2 \times 42 + 21 & \gcd(105, 42) &= \gcd(42, 21) \\ 42 &= 2 \times 21 + 0 & \gcd(42, 21) &= \gcd(21, 0) = 21 \end{aligned}

Therefore, gcd(252,105)=21\gcd(252, 105) = 21.

Verification: 252=21×12252 = 21 \times 12 and 105=21×5105 = 21 \times 5, and gcd(12,5)=1\gcd(12, 5) = 1. ✓

The Extended Euclidean Algorithm: Finding Linear Combinations

The Extended Euclidean Algorithm not only computes gcd(a,b)\gcd(a, b) but also finds integers xx and yy satisfying Bézout's Identity:

ax+by=gcd(a,b)ax + by = \gcd(a, b)

Algorithm:

python
def extended_gcd(a, b):
    if b == 0:
        return a, 1, 0
    else:
        gcd, x1, y1 = extended_gcd(b, a % b)
        x = y1
        y = x1 - (a // b) * y1
        return gcd, x, y

Mathematical Justification: We maintain the invariant that at each recursive call, we can express the GCD as a linear combination of the current arguments.

Extended Algorithm Example: gcd(252,105)\gcd(252, 105)

Working through our previous example:

252=2×105+42105=2×42+2142=2×21+0\begin{aligned} 252 &= 2 \times 105 + 42 \\ 105 &= 2 \times 42 + 21 \\ 42 &= 2 \times 21 + 0 \end{aligned}

Back-substitution:

21=1052×42=1052×(2522×105)=1052×252+4×105=5×1052×252=(2)×252+5×105\begin{aligned} 21 &= 105 - 2 \times 42 \\ &= 105 - 2 \times (252 - 2 \times 105) \\ &= 105 - 2 \times 252 + 4 \times 105 \\ &= 5 \times 105 - 2 \times 252 \\ &= (-2) \times 252 + 5 \times 105 \end{aligned}

Result: 252×(2)+105×5=21=gcd(252,105)252 \times (-2) + 105 \times 5 = 21 = \gcd(252, 105)

Verification: 252×(2)+105×5=504+525=21252 \times (-2) + 105 \times 5 = -504 + 525 = 21

Coprimality: When Numbers Share Nothing

Definition

Two integers aa and bb are coprime (or relatively prime) if:

gcd(a,b)=1\gcd(a, b) = 1

This means they share no common prime factors.

Examples:

  • gcd(15,28)=1\gcd(15, 28) = 1, so 1515 and 2828 are coprime
  • gcd(12,18)=61\gcd(12, 18) = 6 \neq 1, so 1212 and 1818 are not coprime

Properties of Coprime Numbers

Theorem: If gcd(a,b)=1\gcd(a, b) = 1 and abca \mid bc, then aca \mid c.

Proof: Since gcd(a,b)=1\gcd(a, b) = 1, there exist integers x,yx, y such that ax+by=1ax + by = 1. Multiplying by cc:

acx+bcy=cacx + bcy = c

Since abca \mid bc, we have bc=akbc = ak for some integer kk. Substituting:

acx + aky = c \implies a(cx + ky) = c

Therefore aca \mid c. \square

Corollary: If pp is prime and pabp \mid ab, then pap \mid a or pbp \mid b.

The Least Common Multiple: Finding Common Multiples

Definition

The Least Common Multiple of two positive integers aa and bb is the smallest positive integer that is divisible by both aa and bb.

Notation: We write this as lcm(a,b)\text{lcm}(a, b) or [a,b][a, b].

Formal Definition:

lcm(a,b)=min{mZ+:am and bm}\text{lcm}(a, b) = \min\{m \in \mathbb{Z}^+ : a \mid m \text{ and } b \mid m\}

Examples:

  • lcm(12,18)=36\text{lcm}(12, 18) = 36 because 3636 is the smallest positive multiple of both 1212 and 1818
  • lcm(7,11)=77\text{lcm}(7, 11) = 77 because 77 and 1111 are coprime

The Fundamental Relationship

The most important relationship between GCD and LCM is:

gcd(a,b)×lcm(a,b)=a×b\gcd(a, b) \times \text{lcm}(a, b) = |a \times b|

Proof: Let d=gcd(a,b)d = \gcd(a, b), so a=daa = da' and b=dbb = db' where gcd(a,b)=1\gcd(a', b') = 1.

Then:

lcm(a,b)=lcm(da,db)=d×lcm(a,b)=d×ab=abd\text{lcm}(a, b) = \text{lcm}(da', db') = d \times \text{lcm}(a', b') = d \times a'b' = \frac{ab}{d}

Therefore:

gcd(a,b)×lcm(a,b)=d×abd=ab\gcd(a, b) \times \text{lcm}(a, b) = d \times \frac{ab}{d} = ab

This gives us the computational formula:

lcm(a,b)=a×bgcd(a,b)\text{lcm}(a, b) = \frac{|a \times b|}{\gcd(a, b)}

Computing LCM Efficiently

Algorithm:

  1. Compute gcd(a,b)\gcd(a, b) using the Euclidean algorithm
  2. Apply the formula: lcm(a,b)=abgcd(a,b)\text{lcm}(a, b) = \frac{|ab|}{\gcd(a, b)}

Example: Find lcm(252,105)\text{lcm}(252, 105)

We already know gcd(252,105)=21\gcd(252, 105) = 21, so:

lcm(252,105)=252×10521=2646021=1260\text{lcm}(252, 105) = \frac{252 \times 105}{21} = \frac{26460}{21} = 1260

Verification:

  • 1260=252×51260 = 252 \times 5
  • 1260=105×121260 = 105 \times 12
  • No smaller positive integer is divisible by both 252252 and 105105

Properties of LCM

Property 1 (Symmetry):

lcm(a,b)=lcm(b,a)\text{lcm}(a, b) = \text{lcm}(b, a)

Property 2 (Associativity):

lcm(a,lcm(b,c))=lcm(lcm(a,b),c)\text{lcm}(a, \text{lcm}(b, c)) = \text{lcm}(\text{lcm}(a, b), c)

Property 3 (Distributivity with GCD):

lcm(a,gcd(b,c))=gcd(lcm(a,b),lcm(a,c))\text{lcm}(a, \gcd(b, c)) = \gcd(\text{lcm}(a, b), \text{lcm}(a, c))

Property 4 (Coprime Case):

If gcd(a,b)=1, then lcm(a,b)=ab\text{If } \gcd(a, b) = 1, \text{ then } \text{lcm}(a, b) = ab

Prime Factorization Approach

GCD via Prime Factorization

If we have the prime factorizations:

a=p1a1p2a2pkaka = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}

b=p1b1p2b2pkbkb = p_1^{b_1} p_2^{b_2} \cdots p_k^{b_k}

Then:

gcd(a,b)=p1min(a1,b1)p2min(a2,b2)pkmin(ak,bk)\gcd(a, b) = p_1^{\min(a_1, b_1)} p_2^{\min(a_2, b_2)} \cdots p_k^{\min(a_k, b_k)}

lcm(a,b)=p1max(a1,b1)p2max(a2,b2)pkmax(ak,bk)\text{lcm}(a, b) = p_1^{\max(a_1, b_1)} p_2^{\max(a_2, b_2)} \cdots p_k^{\max(a_k, b_k)}

Example: Find gcd(72,48)\gcd(72, 48) and lcm(72,48)\text{lcm}(72, 48)

Prime factorizations:

  • 72=23×3272 = 2^3 \times 3^2
  • 48=24×3148 = 2^4 \times 3^1

Therefore:

  • gcd(72,48)=2min(3,4)×3min(2,1)=23×31=24\gcd(72, 48) = 2^{\min(3,4)} \times 3^{\min(2,1)} = 2^3 \times 3^1 = 24
  • lcm(72,48)=2max(3,4)×3max(2,1)=24×32=144\text{lcm}(72, 48) = 2^{\max(3,4)} \times 3^{\max(2,1)} = 2^4 \times 3^2 = 144

Verification: gcd(72,48)×lcm(72,48)=24×144=3456=72×48\gcd(72, 48) \times \text{lcm}(72, 48) = 24 \times 144 = 3456 = 72 \times 48

Multiple Numbers: Extending the Concepts

GCD of Multiple Numbers

For nn numbers a1,a2,,ana_1, a_2, \ldots, a_n:

gcd(a1,a2,,an)=gcd(gcd(a1,a2),a3,,an)\gcd(a_1, a_2, \ldots, a_n) = \gcd(\gcd(a_1, a_2), a_3, \ldots, a_n)

Algorithm:

python
def gcd_multiple(numbers):
    result = numbers[0]
    for i in range(1, len(numbers)):
        result = gcd(result, numbers[i])
        if result == 1:
            break  # Early termination optimization
    return result

LCM of Multiple Numbers

Similarly:

lcm(a1,a2,,an)=lcm(lcm(a1,a2),a3,,an)\text{lcm}(a_1, a_2, \ldots, a_n) = \text{lcm}(\text{lcm}(a_1, a_2), a_3, \ldots, a_n)

Important: The relationship gcd×lcm=product\gcd \times \text{lcm} = \text{product} only holds for two numbers!

Cryptographic Applications

RSA Key Generation

In RSA cryptography, we need:

  1. Prime Selection: Choose primes pp and qq
  2. Totient Computation: φ(n)=(p1)(q1)\varphi(n) = (p-1)(q-1) where n=pqn = pq
  3. Public Exponent: Choose ee such that gcd(e,φ(n))=1\gcd(e, \varphi(n)) = 1
  4. Private Exponent: Find dd such that ed \equiv 1 \pmod{\varphi(n)}

The Extended Euclidean Algorithm is used in step 4 to find the modular inverse.

Modular Arithmetic

Finding Multiplicative Inverses: To find a^{-1} \bmod n:

  1. Use Extended Euclidean Algorithm to find x,yx, y such that ax+ny=gcd(a,n)ax + ny = \gcd(a, n)
  2. If gcd(a,n)=1\gcd(a, n) = 1, then x \equiv a^{-1} \pmod{n}
  3. If gcd(a,n)1\gcd(a, n) \neq 1, the inverse doesn't exist

Chinese Remainder Theorem

The CRT requires pairwise coprime moduli. We use GCD to verify:

gcd(ni,nj)=1for all ij\gcd(n_i, n_j) = 1 \quad \text{for all } i \neq j

Diffie-Hellman Key Exchange

When selecting parameters for Diffie-Hellman:

  1. Choose prime pp
  2. Find primitive root gg modulo pp
  3. Verify gcd(g(p1)/q,p)=1\gcd(g^{(p-1)/q}, p) = 1 for security

Advanced Topics

Stein's Algorithm (Binary GCD)

For computer implementation, Stein's Algorithm can be more efficient as it uses only subtraction and bit shifts:

python
def binary_gcd(a, b):
    if a == 0: return b
    if b == 0: return a
    
    # Remove common factors of 2
    shift = 0
    while ((a | b) & 1) == 0:
        a >>= 1
        b >>= 1
        shift += 1
    
    # Remove remaining factors of 2 from a
    while (a & 1) == 0:
        a >>= 1
    
    while b != 0:
        # Remove factors of 2 from b
        while (b & 1) == 0:
            b >>= 1
        
        # Ensure a ≤ b
        if a > b:
            a, b = b, a
        
        b = b - a
    
    return a << shift

Computational Complexity

AlgorithmTime ComplexitySpace Complexity
EuclideanO(logmin(a,b))O(\log \min(a,b))O(1)O(1)
Extended EuclideanO(logmin(a,b))O(\log \min(a,b))O(logmin(a,b))O(\log \min(a,b))
Binary GCDO(log2min(a,b))O(\log^2 \min(a,b))O(1)O(1)
Prime FactorizationO(max(a,b))O(\sqrt{\max(a,b)})O(logmax(a,b))O(\log \max(a,b))

Bézout's Identity and Linear Diophantine Equations

The equation ax+by=cax + by = c has integer solutions if and only if gcd(a,b)c\gcd(a, b) \mid c.

General Solution: If (x0,y0)(x_0, y_0) is a particular solution to ax+by=gcd(a,b)ax + by = \gcd(a, b), then all solutions to ax+by=cax + by = c are:

x=x0cgcd(a,b)+kbgcd(a,b)x = x_0 \cdot \frac{c}{\gcd(a, b)} + k \cdot \frac{b}{\gcd(a, b)}

y=y0cgcd(a,b)kagcd(a,b)y = y_0 \cdot \frac{c}{\gcd(a, b)} - k \cdot \frac{a}{\gcd(a, b)}

for any integer kk.

Practical Examples and Applications

Example 1: Simplifying Fractions

To simplify 252105\frac{252}{105}:

252105=252÷gcd(252,105)105÷gcd(252,105)=252÷21105÷21=125\frac{252}{105} = \frac{252 \div \gcd(252, 105)}{105 \div \gcd(252, 105)} = \frac{252 \div 21}{105 \div 21} = \frac{12}{5}

Example 2: Finding Common Denominators

To add 512+718\frac{5}{12} + \frac{7}{18}, we need lcm(12,18)\text{lcm}(12, 18):

gcd(12,18)=6,lcm(12,18)=12×186=36\gcd(12, 18) = 6, \quad \text{lcm}(12, 18) = \frac{12 \times 18}{6} = 36

512+718=1536+1436=2936\frac{5}{12} + \frac{7}{18} = \frac{15}{36} + \frac{14}{36} = \frac{29}{36}

Example 3: Cryptographic Key Validation

In RSA with p=61,q=53p = 61, q = 53:

n=pq=3233,φ(n)=(p1)(q1)=60×52=3120n = pq = 3233, \quad \varphi(n) = (p-1)(q-1) = 60 \times 52 = 3120

Choose e=17e = 17. Check: \gcd(17, 3120) = \gcd(17, 3120 \bmod 17) = \gcd(17, 8) = 1 ✓

Find dd: 17d \equiv 1 \pmod{3120}

Using Extended Euclidean Algorithm: d=2753d = 2753

Verification: 17×2753=46801=15×3120+117 \times 2753 = 46801 = 15 \times 3120 + 1

Summary of Key Formulas

ConceptFormula
GCD-LCM Relationshipgcd(a,b)×lcm(a,b)=ab\gcd(a,b) \times \text{lcm}(a,b) = |ab|
LCM Computationlcm(a,b)=abgcd(a,b)\text{lcm}(a,b) = \frac{|ab|}{\gcd(a,b)}
Bézout's Identityax+by=gcd(a,b)ax + by = \gcd(a,b)
Coprimalitygcd(a,b)=1\gcd(a,b) = 1
Euclidean Step\gcd(a,b) = \gcd(b, a \bmod b)

Practice Problems

  1. Basic Computation: Find gcd(1071,462)\gcd(1071, 462) and lcm(1071,462)\text{lcm}(1071, 462)

  2. Extended Algorithm: Find integers x,yx, y such that 1071x+462y=gcd(1071,462)1071x + 462y = \gcd(1071, 462)

  3. Cryptographic Application: For RSA with p=17,q=19p = 17, q = 19, find a valid public exponent ee and corresponding private exponent dd

  4. Multiple Numbers: Find gcd(48,72,96)\gcd(48, 72, 96) and lcm(48,72,96)\text{lcm}(48, 72, 96)

  5. Diophantine Equation: Solve 15x+25y=515x + 25y = 5 for integers x,yx, y

Solutions

  1. Solution:

    1071 = 2 × 462 + 147
    462 = 3 × 147 + 21
    147 = 7 × 21 + 0
    
    gcd(1071, 462) = 21
    lcm(1071, 462) = (1071 × 462) ÷ 21 = 23562
  2. Solution: Working backwards from the Euclidean algorithm:

    21 = 462 - 3 × 147
    21 = 462 - 3 × (1071 - 2 × 462)
    21 = 7 × 462 - 3 × 1071
    
    Therefore: x = -3, y = 7

The beauty of GCD and LCM lies in their fundamental simplicity combined with their profound applications. From ancient Greek mathematics to modern cryptographic systems, these concepts continue to be essential tools for understanding the deep structure of numbers and building secure digital systems.

Released under the MIT License.