Leandro Junes

Extended Euclidean Algorithm

Computes gcd(a, b) and integers x, y with a·x + b·y = gcd(a, b), with every step of the algorithm listed so a hand calculation can be checked against it.

Inputs

Any integer.

Any integer.

Try

Result

—

gcd(240, 46)

2

240 · (-9) + 46 · (47) = 2

x
-9
y
47
Steps
5

Algorithm steps

—
i q r x y

Every row satisfies r = a·x + b·y, working from |a| and |b|. The last nonzero r is gcd(a, b); its x and y (sign-adjusted for negative inputs) are the Bézout coefficients above.