(1)判断是否是偶数,如果都是偶数,则对两个数进行//2操作
(2)上述不成立则:t = max(a,b) - min(a,b)
if t == min(a,b):
返回最大公因数t
else:
继续上述步骤
t' = min(t,min(a,b))
def gys(a,b):
while a %2 == 0 and b%2 == 0:
a = a//2
b = b //2
maxa = max(a,b)
mina = min(a,b)
t = maxa - mina
while t != mina:
maxa = max(t, mina)
mina = min(t,mina)
t = maxa - mina
print(a,b)
return t
print(gys(98,63))
2、欧几里得方法
连除法:
给出两个数:a,b
找出两个数中的max和min
max %min = A
min % A = B
A % B = C
直到余数为0
则被除数为最大
def gys(a,b):
a = max(a,b)
b = min(a,b)
if a % b == 0:
print(b)
else:
t= b
b = a % b
a = t
gys(a,b)
gys(9,3)