Extended Euclidean Algorithm Calculator
Find the gcd of two integers and the Bezout coefficients that express it as a linear combination.
What it does
The Euclidean algorithm finds the greatest common divisor of two integers by repeated division. The extended version also tracks how each remainder is built from the original numbers, which produces two integers called Bezout coefficients.
This tool takes the two integers and returns the gcd together with coefficients x and y such that a times x plus b times y equals that gcd. A check row shows the computed combination so you can verify the identity immediately.
The page opens with a = 240 and b = 46. The gcd is 2, and one valid pair is x = minus 9 and y = 47, because 240 times minus 9 plus 46 times 47 equals 2.
The algorithm keeps a running remainder and two coefficient pairs, updating them at each division step. A guard limits the loop so hostile or unusual inputs cannot cause endless work.
When the gcd is 1 the coefficient x is the multiplicative inverse of a modulo b, up to sign. That is the main reason the extended algorithm matters in modular arithmetic and cryptography.
How to use
- Enter the first integer a.
- Enter the second integer b.
- Read the gcd as the main result.
- Check the Bezout coefficients x and y.
- Verify the identity using the check row.
- Try coprime numbers such as 35 and 27 to see a gcd of 1.
Formula
- a, b
- The two input integers.
- gcd
- The greatest common divisor, always nonnegative.
- x, y
- Integers satisfying a times x plus b times y equals the gcd.
Starting with x = 1, y = 0 for a and x = 0, y = 1 for b, each division step subtracts the quotient times the current coefficient pair from the previous pair. When the remainder reaches zero, the previous remainder is the gcd and the previous coefficients give the identity.
Worked example
Example: 240 and 46
The page opens with a = 240 and b = 46.
- Divide 240 by 46 to get quotient 5 and remainder 10.
- Divide 46 by 10 to get quotient 4 and remainder 6.
- Divide 10 by 6 to get quotient 1 and remainder 4.
- Divide 6 by 4 to get quotient 1 and remainder 2.
- Divide 4 by 2 to get quotient 2 and remainder 0, so the gcd is 2.
- Back substituting gives x = minus 9 and y = 47.
- Check: 240 times minus 9 plus 46 times 47 is minus 2,160 plus 2,162, which is 2.
Reference table
| a | b | gcd | x | y |
|---|---|---|---|---|
| 240 | 46 | 2 | -9 | 47 |
| 35 | 27 | 1 | -10 | 13 |
| 99 | 78 | 3 | -11 | 14 |
Limits
- Inputs must be whole numbers between minus 10 to the 12th power and 10 to the 12th power.
- At least one input must be nonzero.
- The returned coefficients are one valid pair, not the only pair.
- The loop is capped at 200 iterations.
Common mistakes
- Thinking the coefficients are unique.
- Forgetting that the gcd is reported as a positive number.
- Confusing the Euclidean and extended Euclidean outputs.
- Using the coefficient y as a modular inverse without reducing modulo b.
- Entering non integer inputs.
Try it yourself
Quick checks. Answers sit tucked under each question.
Find gcd and coefficients for 35 and 27.
gcd 1, with x = minus 10 and y = 13 because 35 times minus 10 plus 27 times 13 equals 1.
Find gcd and coefficients for 99 and 78.
gcd 3, with x = minus 11 and y = 14.
What do coprime inputs give?
A gcd of 1 and a linear combination equal to 1.
Questions
What are Bezout coefficients?
Integers x and y such that a times x plus b times y equals the gcd of a and b.
Are the coefficients unique?
No. Adding multiples of b divided by the gcd to x and corresponding multiples to y gives another pair.
Why is this useful?
It gives modular inverses when the inputs are coprime and helps solve linear Diophantine equations.
Can inputs be negative?
Yes. The gcd is reported positive and the coefficient signs are adjusted.
What if both inputs are zero?
The gcd is undefined and the tool reports an error.
How is the result checked?
Multiply a by x and b by y and add. The check row shows that the total equals the gcd.
Sources
- University of Colorado, Math 3110 summary of linear Diophantine equations
- Stanford University crypto notes, extended Euclidean algorithm
- University of Washington CSE 311 lecture on the extended Euclidean algorithm