Back to Data Structures & Algorithms
2025-12-225 min read

extended Euclidean algorithm (Data Structures & Algorithms)

Learn extended Euclidean algorithm (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

The Extended Euclidean Algorithm is an essential tool in the field of number theory and competitive programming. It allows us to find the greatest common divisor (GCD), solve linear Diophantine equations, and compute modular inverses efficiently. Understanding this algorithm will significantly enhance your problem-solving skills and improve your performance in coding challenges.

Prerequisites

Before diving into the Extended Euclidean Algorithm, you should be familiar with:

  1. Basic Python syntax and control structures (loops, conditionals)
  2. Understanding of modular arithmetic
  3. Familiarity with recursion and dynamic programming techniques
  4. Knowledge of Big O notation to understand the time complexity of algorithms
  5. A basic understanding of number theory concepts, such as prime numbers and congruences

Core Concept

The Extended Euclidean Algorithm is an extension of the Euclidean Algorithm, which computes the greatest common divisor (GCD) of two numbers. The extended version also finds coefficients x and y such that:

a * x + b * y = gcd(a, b)

Here's a step-by-step breakdown of the algorithm:

  1. If b == 0, then gcd(a, b) = a. Set x = 1 and y = 0.
  2. Otherwise, apply the Extended Euclidean Algorithm to b and the remainder r = a % b.
  3. Calculate new coefficients x1, y1, x2, and y2 as follows:
  • x1 = y
  • y1 = x - (a // b) * y
  • x2 = x
  • y2 = y1
  1. Replace (a, b) with (b, r), and repeat the process until b == 0.
  2. The final coefficients x and y will satisfy:
a * x + b * y = gcd(a, b)

Worked Example

Let's find x and y such that:

a = 25
b = 18
gcd_ab = gcd_extended(a, b) # Implement the Extended Euclidean Algorithm here
print("gcd(25, 18) =", gcd_ab)
print("x =", x)
print("y =", y)

After implementing the algorithm, you'll find that gcd(25, 18) = 3, and x = -3 and y = 4.

Implementing the Extended Euclidean Algorithm in Python

Here's a recursive implementation of the Extended Euclidean Algorithm:

def gcd_extended(a, b):
if b == 0:
x = 1
y = 0
return a, x, y

q = a // b
gcd, x1, y1 = gcd_extended(b, a % b)
x = y1
y = x1 - q * y

return gcd, x, y

How It Works Internally

In Python, the Extended Euclidean Algorithm can be implemented recursively or iteratively. The iterative version is more efficient in terms of memory usage but might be slower due to the need to keep track of intermediate results. On the other hand, the recursive implementation is faster but consumes more memory due to the deep call stack.

Common Mistakes

  1. Forgetting to initialize x and y when b == 0.
  2. Miscalculating coefficients x1, y1, x2, and y2.
  3. Implementing an inefficient version of the algorithm (e.g., using a loop instead of recursion or vice versa).
  4. Not handling edge cases, such as negative values for x and y.
  5. Failing to return both gcd(a, b) and coefficients x and y.
  6. Implementing an incorrect base case (when b == 0, the algorithm should return a, not b).
  7. Not considering the possibility of negative values for x and y.
  8. Failing to handle large inputs efficiently, which may lead to exceeding the maximum recursion depth or consuming excessive memory.

Practice Questions

  1. Write a Python function that finds the GCD of two numbers using the Extended Euclidean Algorithm and returns the coefficients x and y such that:
a * x + b * y = gcd(a, b)
  1. Solve the following linear Diophantine equation using the Extended Euclidean Algorithm:
13x + 8y = 1
  1. Implement an iterative version of the Extended Euclidean Algorithm in Python.
  2. Implement a recursive version of the Extended Euclidean Algorithm that handles large inputs efficiently by using memoization or dynamic programming techniques.
  3. Write a function to compute the modular inverse of a modulo m using the Extended Euclidean Algorithm:
def mod_inverse(a, m):
gcd, x = gcd_extended(a, m)
if gcd != 1:
raise ValueError("Modular inverse does not exist.")
return pow(x, m - 2, m)
  1. Write a function to find the least common multiple (LCM) of two numbers using the Extended Euclidean Algorithm:
def lcm(a, b):
gcd, x, y = gcd_extended(a, b)
return abs(a * b) // gcd
  1. Write a function to find all solutions of the linear Diophantine equation ax + by = c, where c is a positive integer:
def diophantine_solutions(a, b, c):
gcd, x, y = gcd_extended(a, b)
if gcd != 1:
raise ValueError("The equation has no solution.")
solutions = []
for i in range(c // abs(gcd)):
solutions.append((i * x, i * y))
return solutions

FAQ

Why do we need coefficients x and y when finding the GCD?

  • Coefficients x and y are useful for solving linear Diophantine equations, computing modular inverses, and other number-theoretic problems.

What is the time complexity of the Extended Euclidean Algorithm?

  • The time complexity of both the recursive and iterative versions of the Extended Euclidean Algorithm is O(log N), where N is the maximum of the input numbers.

Can we use the Extended Euclidean Algorithm to find the least common multiple (LCM) of two numbers?

  • Yes, we can compute the LCM using the coefficients x and y found by the Extended Euclidean Algorithm: lcm(a, b) = |a * b| / gcd(a, b).

How does the Extended Euclidean Algorithm relate to Bezout's Identity?

  • The Extended Euclidean Algorithm provides a method for finding integers x and y that satisfy Bezout's Identity: gcd(a, b) = a * x + b * y.

Is it possible to find integer solutions for any linear Diophantine equation using the Extended Euclidean Algorithm?

  • Yes, if the GCD of the coefficients is 1, then there exist integers x and y that satisfy the equation ax + by = c. The Extended Euclidean Algorithm can be used to find these solutions.

How can we use the Extended Euclidean Algorithm to solve quadratic Diophantine equations?

  • Quadratic Diophantine equations are more complex and typically require additional techniques, such as completing the square or using elliptic curves. The Extended Euclidean Algorithm alone cannot solve quadratic Diophantine equations directly. However, it can be used to find integer solutions for linear Diophantine equations, which may help in solving certain types of quadratic Diophantine equations indirectly.
extended Euclidean algorithm (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn