最大公约数(Greatest Common Divisor):指两个或多个整数共有约数中最大的一个
最小公倍数=两数之积/最大公约数
辗转相除法(递归的形式)- Python
# 自定义"最大公约数"(辗转相除法-递归法)
def gcd(a, b):
temp = a % b
if temp > 0:
return gcd(b, temp)
else:
return b
# 自定义“最小公倍数”(最小公倍数=两数之积/最大公约数)
def lcm(a, b):
return a * b / gcd(a, b)
辗转相除法(递归的形式)- GO
# 自定义"最大公约数"(辗转相除法-递归法)
func gcd(a, b int) int {
temp := a % b
if temp > 0 {
return gcd(b, temp)
}else {
return b
}
}
# 自定义“最小公倍数”(最小公倍数=两数之积/最大公约数)
func lcm(a, b int) int{
return a * b / gcd(a, b)
}