Calculator for the Euclidean algorithm and extended calculation with verified Bézout coefficients

Euclidean Algorithm Calculator: Check Bézout Coefficients

Finding a greatest common divisor is not enough when a program also needs coefficients that produce it. A calculator for the Euclidean algorithm can find the divisor, while the extended calculation tracks how to express it using the original integers.

Use an extended calculator for the Euclidean algorithm, or a short Python implementation, to obtain a result you can verify by substitution. For this example, take a = 240 and b = 46.

Extended Euclidean Algorithm Calculator in Python

The following extended Euclidean algorithm calculator returns (g, x, y) for nonnegative integers, provided at least one is nonzero. It keeps track of the coefficients as it performs the divisions.

def extended_gcd(a, b):
    if a < 0 or b < 0 or (a == 0 and b == 0):
        raise ValueError("Use nonnegative integers, not both zero")
    old_r, r = a, b
    old_x, x = 1, 0
    old_y, y = 0, 1

    while r != 0:
        q = old_r // r
        old_r, r = r, old_r - q * r
        old_x, x = x, old_x - q * x
        old_y, y = y, old_y - q * y

    return old_r, old_x, old_y

a, b = 240, 46
g, x, y = extended_gcd(a, b)
print(f"g={g}, x={x}, y={y}")
print(f"check: {a}*({x}) + {b}*({y}) = {a*x + b*y}")

Save it as extended_gcd.py and run python3 extended_gcd.py in a terminal. The output is:

g=2, x=-9, y=47
check: 240*(-9) + 46*(47) = 2

Select and copy the output from the terminal into your notes, keeping the inputs with it. For another pair, change a and b and run the file again. If one input is zero, this version returns the other input as the divisor and a corresponding coefficient pair. Both zero inputs are rejected because this example needs a nonzero divisor for its later formulas. The explicit input restriction keeps the example’s sign convention clear; a production routine can support negative integers with an appropriate normalization rule.

Follow the divisions

The ordinary Euclidean algorithm repeatedly divides the previous divisor by the remainder:

240 = 5 × 46 + 10
 46 = 4 × 10 + 6
 10 = 1 × 6  + 4
  6 = 1 × 4  + 2
  4 = 2 × 2  + 0

The last nonzero remainder is 2, so gcd(240, 46) = 2. An extended Euclidean calculation retains the relationship between each remainder and the two starting values. The script does that through the coefficient pairs updated inside the loop. At each iteration, old_r = old_x*a + old_y*b and r = x*a + y*b. Subtracting the same multiple of each coefficient pair preserves those identities while reducing the remainder. When the second remainder becomes zero, the first is the divisor and its associated pair supplies the answer. This invariant is useful when reviewing or testing an implementation: checking only the final divisor would miss a broken coefficient update.

Back-substitution gives the same result. Starting with 2 = 6 - 4, replace 4 by 10 - 6, then replace 6 by 46 - 4 × 10. Finally replace 10 by 240 - 5 × 46. This gives 2 = -9 × 240 + 47 × 46.

Interpret more than one answer

The coefficients are not unique. For nonzero g = gcd(a, b), if (x, y) is one solution, then (x + k*b/g, y - k*a/g) is another for any integer k. With k = 1, the example also yields (14, -73), and 240 × 14 + 46 × (-73) = 2.

A different coefficient pair from an extended Euclidean calculator is therefore not automatically wrong. Check that the returned divisor is the greatest common divisor and substitute the coefficients in the original order. If the result is 1, the coefficient of a also supplies an inverse of a modulo b when b > 1.

Keep these steps handy:

  • Record the two inputs and their order.
  • Find the last nonzero remainder and retain the coefficient pair.
  • Substitute into a*x + b*y = g.
  • Accept a different pair only after checking the same identity and divisor.