algorithm-in-python/math/numberTheory/gcd.py

39 lines
808 B
Python
Raw Normal View History

#coding: utf-8
''' mbinary
#######################################################################
# File : gcd.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
2019-01-31 12:09:46 +08:00
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-12-16 10:06
# Description:
#######################################################################
'''
2020-04-15 12:28:20 +08:00
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
2020-04-15 12:28:20 +08:00
def lcm(a, b):
return int(a*b/gcd(a, b))
def xgcd(a, b):
'''return gcd(a,b), x,y where ax+by=gcd(a,b)'''
2020-04-15 12:28:20 +08:00
if b == 0:
return a, 1, 0
g, x, y = xgcd(b, a % b)
return g, y, x-a//b*y
2020-04-15 12:28:20 +08:00
if __name__ == '__main__':
while 1:
a = int(input('a: '))
b = int(input('b: '))
2020-04-15 12:28:20 +08:00
print('gcd :', gcd(a, b))
print('xgcd:', xgcd(a, b))