-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathgcd.py
38 lines (30 loc) · 808 Bytes
/
gcd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#coding: utf-8
''' mbinary
#######################################################################
# File : gcd.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-12-16 10:06
# Description:
#######################################################################
'''
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
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)'''
if b == 0:
return a, 1, 0
g, x, y = xgcd(b, a % b)
return g, y, x-a//b*y
if __name__ == '__main__':
while 1:
a = int(input('a: '))
b = int(input('b: '))
print('gcd :', gcd(a, b))
print('xgcd:', xgcd(a, b))