最大公约数
//更相减损法
def func(num1, num2):
while True:
if num1 > num2:
num1 -= num2
elif num1 < num2:
num2 -= num1
else:
return num1
--------------------------
gcd(a, b) = gcd(b, a mod b)
递归算法:
def gcd(x, y):
if y == 0:
return x
return gcd(y, x % y)
-----------------------
return gcd(y, x % y) if y else x
-----------------------
最小公倍数
//利用 最小公倍数 = 两个数乘积 / 最大公约数
//最小公倍数记为[a, b]
//最大公约数记为(a, b)
//定理: (a, b) * [a, b] = ab
def func(num1, num2):
num_mul = num1 * num2
while True:
if num1 > num2:
num1 -= num2
elif num1 < num2:
num2 -= num1
else:
return int(num_mul / num1)