Introduction to Euclidean Algorithm

The Euclidean Algorithm is one of the oldest algorithms still in common use. Named after the ancient Greek mathematician Euclid, who described it in his Elements (c. 300 BC), this algorithm efficiently computes the greatest common divisor (GCD) of two integers.

Historical Significance

The Euclidean Algorithm predates computers by over two millennia but remains one of the most efficient algorithms for computing GCDs. Its applications span from ancient Greek mathematics to modern cryptography and computer science.

🏛️

Ancient Origins

First described by Euclid in Book VII of his Elements around 300 BC. Used by ancient Greek mathematicians for number theory and Number-Theory.

Efficiency

One of the first algorithms analyzed for computational complexity. Runs in O(log min(a, b)) time, making it extremely fast even for large numbers.

🔐

Modern Applications

Essential for RSA encryption, modular arithmetic, and computer algebra systems. Forms the basis for many cryptographic protocols.

Why Learn the Euclidean Algorithm?
  • Fundamental Concept: Core to number theory and algebra
  • Practical Applications: Used in cryptography, computer science, and engineering
  • Algorithmic Thinking: Excellent example of efficient algorithm design
  • Mathematical Beauty: Simple yet powerful mathematical idea
  • Historical Importance: One of the oldest algorithms still in use

Improve your understanding by working through practical tasks with the gcf-calculator.

What is Greatest Common Divisor (GCD)?

The greatest common divisor (GCD) of two integers is the largest positive integer that divides both numbers without leaving a remainder. Also known as the greatest common factor (GCF) or highest common factor (HCF).

gcd(a, b) = max{d ∈ ℕ : d | a and d | b}

Where:

  • a, b are integers (not both zero)
  • d | a means "d divides a" (a is divisible by d)
  • represents the set of natural numbers (positive integers)

Examples:

gcd(48, 18) = 6 because 6 is the largest number dividing both 48 and 18

gcd(17, 5) = 1 (17 and 5 are coprime)

gcd(0, 5) = 5 (by definition, gcd(a, 0) = |a|)

gcd(60, 84) = 12

Properties of GCD
  1. Commutative: gcd(a, b) = gcd(b, a)
  2. gcd(a, 0): gcd(a, 0) = |a|
  3. gcd with negative numbers: gcd(a, b) = gcd(|a|, |b|)
  4. Distributive: gcd(ka, kb) = |k|·gcd(a, b) for any integer k
  5. Linear combination: gcd(a, b) is the smallest positive integer of the form ax + by
1
Naive Method for Finding GCD

The naive approach to find gcd(a, b) is to:

  1. List all divisors of a
  2. List all divisors of b
  3. Find the largest number appearing in both lists

Example: gcd(48, 18)

Divisors of 48: 1, 2, 3, 4, 6, 8, 12, 16, 24, 48

Divisors of 18: 1, 2, 3, 6, 9, 18

Common divisors: 1, 2, 3, 6

Greatest common divisor: 6

2
Problem with Naive Method

The naive method becomes inefficient for large numbers because:

  • Finding all divisors requires checking up to √n numbers
  • For large numbers (e.g., 100-digit numbers), this is computationally infeasible
  • The Euclidean Algorithm provides a much more efficient solution

Take your learning further by practicing real examples using the gcf-calculator.

The Euclidean Algorithm

The Euclidean Algorithm is based on the principle that the GCD of two numbers does not change if the larger number is replaced by its difference with the smaller number.

Euclid's Division Lemma

For any integers a and b (with b > 0), there exist unique integers q (quotient) and r (remainder) such that:

a = bq + r, where 0 ≤ r < b

The key insight: gcd(a, b) = gcd(b, r)

1
Algorithm Steps

Given two positive integers a and b (with a ≥ b):

  1. If b = 0, then gcd(a, b) = a
  2. Otherwise, compute r = a mod b (remainder when a is divided by b)
  3. Set a = b, b = r
  4. Repeat from step 1
2
Recursive Formulation

The algorithm can be expressed recursively:

function gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a mod b)
3
Iterative Implementation

For computational efficiency, an iterative approach is often used:

function gcd(a, b):
  while b ≠ 0:
    t = b
    b = a mod b
    a = t
  return a
Proof of Correctness

Theorem: For any integers a, b with b > 0, gcd(a, b) = gcd(b, a mod b)

Proof:

  1. Let d = gcd(a, b). Then d divides both a and b.
  2. Write a = bq + r where r = a mod b.
  3. Since d divides a and b, it must also divide r = a - bq.
  4. Thus d divides both b and r, so d ≤ gcd(b, r).
  5. Conversely, let d' = gcd(b, r). Then d' divides both b and r.
  6. Since a = bq + r, d' must also divide a.
  7. Thus d' divides both a and b, so d' ≤ gcd(a, b).
  8. Therefore, gcd(a, b) = gcd(b, r). ∎

Step-by-Step Examples

Let's work through several examples to understand how the Euclidean Algorithm works in practice.

1
Example 1: gcd(48, 18)

Let's compute gcd(48, 18) using the Euclidean Algorithm:

48 = 18 × 2 + 12
Divide 48 by 18, quotient 2, remainder 12
18 = 12 × 1 + 6
Now divide 18 by previous remainder 12
12 = 6 × 2 + 0
Divide 12 by previous remainder 6
gcd(48, 18) = 6
Since remainder is 0, last non-zero remainder is GCD
2
Example 2: gcd(1071, 462)

This example appears in Euclid's original work:

1071 = 462 × 2 + 147
1071 ÷ 462 = 2 remainder 147
462 = 147 × 3 + 21
462 ÷ 147 = 3 remainder 21
147 = 21 × 7 + 0
147 ÷ 21 = 7 remainder 0
gcd(1071, 462) = 21
Last non-zero remainder is 21
3
Example 3: gcd(252, 105)

Let's trace through another example:

252 = 105 × 2 + 42
252 ÷ 105 = 2 remainder 42
105 = 42 × 2 + 21
105 ÷ 42 = 2 remainder 21
42 = 21 × 2 + 0
42 ÷ 21 = 2 remainder 0
gcd(252, 105) = 21
GCD is 21
4
Example 4: Coprime Numbers (gcd = 1)

When numbers are coprime (relatively prime), their GCD is 1:

17 = 5 × 3 + 2
17 ÷ 5 = 3 remainder 2
5 = 2 × 2 + 1
5 ÷ 2 = 2 remainder 1
2 = 1 × 2 + 0
2 ÷ 1 = 2 remainder 0
gcd(17, 5) = 1
17 and 5 are coprime

Try It Yourself

Enter two numbers to see the Euclidean Algorithm in action:

Enter two numbers and click "Calculate GCD" to see the step-by-step process.

Measure your knowledge with real-world exercises on the gcf-calculator.

Extended Euclidean Algorithm

The Extended Euclidean Algorithm not only computes gcd(a, b) but also finds integers x and y such that:

ax + by = gcd(a, b)

This equation is known as Bézout's identity, and the coefficients x and y are called Bézout coefficients.

Bézout's Identity

For any integers a and b (not both zero), there exist integers x and y such that:

ax + by = gcd(a, b)

Moreover, gcd(a, b) is the smallest positive integer that can be expressed in this form.

1
Algorithm Description

The Extended Euclidean Algorithm works by keeping track of coefficients as we perform the standard Euclidean Algorithm:

  1. Initialize: (x₁, y₁) = (1, 0), (x₂, y₂) = (0, 1)
  2. While b ≠ 0:
    • Compute quotient q = a ÷ b (integer division)
    • Compute remainder r = a mod b
    • Update coefficients: (x, y) = (x₁ - qx₂, y₁ - qy₂)
    • Shift: a = b, b = r, x₁ = x₂, y₁ = y₂, x₂ = x, y₂ = y
  3. Result: gcd = a, coefficients = (x₁, y₁)
2
Example: Extended gcd(48, 18)

Let's find x and y such that 48x + 18y = gcd(48, 18) = 6:

48 = 18 × 2 + 12
12 = 48 - 18×2
18 = 12 × 1 + 6
6 = 18 - 12×1
Substitute: 6 = 18 - (48 - 18×2)×1
6 = 18 - 48 + 18×2
Simplify: 6 = -48 + 18×3
6 = 48×(-1) + 18×3
Solution: x = -1, y = 3
Check: 48×(-1) + 18×3 = -48 + 54 = 6 ✓
// Extended Euclidean Algorithm Implementation
function extendedGcd(a, b):
  if b == 0:
    return (a, 1, 0)
  else:
    g, x1, y1 = extendedGcd(b, a mod b)
    x = y1
    y = x1 - (a // b) * y1
    return (g, x, y)

Extended GCD Calculator

Find Bézout coefficients for ax + by = gcd(a, b):

Enter two numbers to find x and y such that ax + by = gcd(a, b).

Applications of Euclidean Algorithm

The Euclidean Algorithm has numerous practical applications beyond just computing GCDs:

🔢

Simplifying Fractions

To simplify a fraction a/b, divide both numerator and denominator by gcd(a, b):

48/18 = (48÷6)/(18÷6) = 8/3

This gives the fraction in lowest terms.

⚖️

Diophantine Equations

Solving linear equations of the form ax + by = c. Solutions exist only if gcd(a, b) divides c.

48x + 18y = 6
Solution: x = -1 + 3k, y = 3 - 8k
🔄

Modular Inverses

Finding modular inverses using Extended Euclidean Algorithm. For a mod m, find x such that:

ax ≡ 1 (mod m)
iff gcd(a, m) = 1
📊

Least Common Multiple

Computing LCM using the relationship with GCD:

lcm(a, b) = |a·b| / gcd(a, b)

Example: lcm(48, 18) = 864/6 = 144

Real-World Applications
Field Application How Euclidean Algorithm Helps
Computer Science Algorithm Analysis Example of efficient recursive algorithm
Cryptography RSA Encryption Finding modular inverses, key generation
Engineering Signal Processing Simplifying rational transfer functions
Mathematics Number Theory Fundamental tool for proofs and computations
Education Teaching Algorithms Classic example of elegant algorithm design

Test and improve your skills using the gcf-calculator.

Applications in Cryptography

The Extended Euclidean Algorithm is fundamental to modern cryptography, particularly in public-key cryptosystems like RSA.

RSA Cryptosystem

RSA (Rivest-Shamir-Adleman) is one of the first practical public-key cryptosystems and is widely used for secure data transmission.

The Extended Euclidean Algorithm is used in RSA to:

  1. Find the modular inverse of e modulo φ(n) during key generation
  2. Compute the private key d such that ed ≡ 1 (mod φ(n))
  3. Ensure that e and φ(n) are coprime (gcd(e, φ(n)) = 1)
1
RSA Key Generation Example

Let's see how the Extended Euclidean Algorithm is used in RSA:

Choose primes: p = 61, q = 53
n = p×q = 3233, φ(n) = (p-1)(q-1) = 3120
Choose e = 17
Check gcd(17, 3120) = 1 ✓
Find d such that 17d ≡ 1 mod 3120
Use Extended Euclidean Algorithm
Compute: 17×2753 = 46801 ≡ 1 mod 3120
So d = 2753
Public key: (n=3233, e=17)
Private key: (n=3233, d=2753)
2
Modular Inverse Calculation

The modular inverse of a modulo m exists if and only if gcd(a, m) = 1. The Extended Euclidean Algorithm finds it:

// Find modular inverse of a mod m
function modInverse(a, m):
  g, x, y = extendedGcd(a, m)
  if g ≠ 1:
    return "No inverse exists"
  else:
    return x mod m
🔐

Diffie-Hellman Key Exchange

Uses modular arithmetic and the fact that finding discrete logarithms is hard. The Euclidean Algorithm helps verify that chosen parameters are valid.

🔑

Elliptic Curve Cryptography

ECC operations involve arithmetic on elliptic curves. The Extended Euclidean Algorithm is used for computing modular inverses in finite fields.

📝

Digital Signatures

Digital signature algorithms like DSA use modular arithmetic and require computing modular inverses, which relies on the Extended Euclidean Algorithm.

🔢

Chinese Remainder Theorem

Used to speed up RSA decryption. The Extended Euclidean Algorithm helps find coefficients for combining solutions from different moduli.

Interactive Euclidean Algorithm Calculator

Complete GCD Calculator

Compute GCD, LCM, Bézout coefficients, and modular inverses all in one tool.

Enter numbers and click "Compute All" to see comprehensive results.

Practice Problems

1. Compute gcd(2520, 1540) using the Euclidean Algorithm.

Solution:

2520 = 1540 × 1 + 980
1540 = 980 × 1 + 560
980 = 560 × 1 + 420
560 = 420 × 1 + 140
420 = 140 × 3 + 0
gcd(2520, 1540) = 140
2. Find integers x and y such that 35x + 15y = gcd(35, 15).

Solution:

35 = 15 × 2 + 5
5 = 35 - 15×2
15 = 5 × 3 + 0
gcd(35, 15) = 5
From first equation: 5 = 35 - 15×2
So x = 1, y = -2
Check: 35×1 + 15×(-2) = 35 - 30 = 5 ✓
3. Find the modular inverse of 7 modulo 26 (if it exists).

Solution:

First check gcd(7, 26) = 1
Inverse exists since they're coprime
26 = 7 × 3 + 5
5 = 26 - 7×3
7 = 5 × 1 + 2
2 = 7 - 5×1
5 = 2 × 2 + 1
1 = 5 - 2×2
Back substitute: 1 = 5 - (7 - 5)×2
1 = 3×5 - 2×7
1 = 3×(26 - 7×3) - 2×7
1 = 3×26 - 11×7
So -11 ≡ 15 mod 26 is the inverse
Check: 7×15 = 105 ≡ 1 mod 26 ✓

Refine your understanding through guided practice with the gcf-calculator.

Time Complexity and Efficiency

The Euclidean Algorithm is remarkably efficient. Its time complexity is O(log min(a, b)), making it suitable for very large numbers.

Lamé's Theorem

Gabriel Lamé proved that the number of division steps in the Euclidean Algorithm for two numbers is at most five times the number of digits in the smaller number.

More precisely, for a ≥ b > 0, the number of steps is ≤ 5·log₁₀b.

Method Time Complexity Steps for 100-digit numbers Practicality
Naive (trial division) O(√n) ~10⁵⁰ operations Impossible
Euclidean Algorithm O(log n) ~500 operations Instant
Binary GCD Algorithm O(log n) ~500 operations Very fast
Extended Euclidean O(log n) ~500 operations Very fast
1
Why So Efficient?

The Euclidean Algorithm reduces numbers quickly because:

  • Each step reduces the larger number by at least half (in the worst case)
  • Fibonacci numbers represent the worst-case scenario
  • For Fibonacci numbers Fₙ, Fₙ₊₁: gcd(Fₙ, Fₙ₊₁) takes n steps
  • Since Fₙ ≈ φⁿ/√5, n ≈ logφ(Fₙ) ≈ 1.44 log₁₀(Fₙ)
2
Binary GCD Algorithm

Also known as Stein's algorithm, this variant uses bitwise operations and is even more efficient on computers:

function binaryGcd(a, b):
  if a == b: return a
  if a == 0: return b
  if b == 0: return a
  if a is even:
    if b is odd: return binaryGcd(a/2, b)
    else: return 2×binaryGcd(a/2, b/2)
  if b is even: return binaryGcd(a, b/2)
  if a > b: return binaryGcd((a-b)/2, b)
  return binaryGcd((b-a)/2, a)

Practice Problems and Exercises

Test your understanding with these practice problems:

1. Compute gcd(314159, 271828) using the Euclidean Algorithm.

Hint: These are the first six digits of π and e respectively. Start with 314159 = 271828 × 1 + 42331

2. Find all integer solutions to 21x + 14y = 70.

Hint: First divide by gcd(21, 14) = 7 to get 3x + 2y = 10. Find one solution using Extended Euclidean Algorithm.

3. Prove that if gcd(a, b) = 1, then gcd(a + b, a - b) is either 1 or 2.

Hint: Let d = gcd(a+b, a-b). Show that d divides 2a and 2b, so d divides gcd(2a, 2b) = 2gcd(a, b) = 2.

4. Implement the Euclidean Algorithm in your favorite programming language.

Python Example:

def gcd(a, b):
  while b != 0:
    a, b = b, a % b
  return abs(a)

def extended_gcd(a, b):
  if b == 0:
    return a, 1, 0
  g, x1, y1 = extended_gcd(b, a % b)
  return g, y1, x1 - (a // b) * y1

Put theory into action by practicing on the gcf-calculator.