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


  1. Let x = v = 1 and y = u = 0.
  2. 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.
  3. Repeat Step 2 until b equals 0.
  4. Return x and y.