Skip to content

Instantly share code, notes, and snippets.

@m00nlight
Created February 14, 2015 13:13
Show Gist options
  • Save m00nlight/56430f21456a23f19ed9 to your computer and use it in GitHub Desktop.
Save m00nlight/56430f21456a23f19ed9 to your computer and use it in GitHub Desktop.
Python extend greatest common divisor
import sys
import itertools
def extend_gcd(a, b):
if b == 0:
return (1, 0, a)
else:
(a1, b1, g) = extend_gcd(b, a % b)
return (b1, a1 - (a // b) * b1, g)
if __name__ == '__main__':
line = sys.stdin.readline()
a,b = line.split()
a,b = int(a), int(b)
(x, y, g) = extend_gcd(a, b)
sys.stdout.write(str(x) + ' ' + str(y) + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment