Description:

  • Let , not both 0. Then, there exist such that theorem
  • Extended Euclidean algorithm
    • Find two integers and such that
    • First use Euclid’s algorithm to find the GCD:
    • From this, the last non-zero remainder (GCD) is .
    • Now we use the extended algorithm:
    • Substituting for 8787 in the first equation, we have

Implementation:

  • def extended_gcd(a, b):
    if b == 0:
    return (a, 1, 0)
    else: s, t, d = extended_gcd(b, a % b)
    return (d, t, s - a // b * t)