There is also an extended version of
Euclid's algorithm,
where integers
x and y are determined such that
ax + by = gcd (a,b).
To do this, we record at each step of
Euclid's algorithm how to
express the intermediate results in the input integers.
The extended Euclidean algorithm
- Input: positive integers a and b.
- Output: integers x and y such that
gcd(a,b) = xa + yb.
- Let x = v = 1 and y = u = 0.
- Determine q and r such that
a = qb + r and 0
r < b.
Replace (simultaneously)
- a by b and b by r,
- x by u and y by v,
- u by x - qu and v by y - qv.
- Repeat Step 2 until b equals 0.
- Return x and y.