特殊数字求解
1. 最大公约数:(辗转相除法)
1 def gcd(a,b): 2 if a%b == 0: 3 return b 4 else : 5 return gcd(b,a%b)
注意:gcd(3,6)=gcd(6,3)
下面的除数可以为0
Specifically, gcd(x, 0) = gcd(0, x) = x.
def gcd(a, b): if b == 0:return a return gcd(b, a % b)
详细解释
def gcd(a, b): if b == 0:return a return gcd(b, a % b) # Specifically, gcd(x, 0) = gcd(0, x) = x. # 即 # print(3%0) #0不能为除数,会出错 print(0%3) print(gcd(3,0),gcd(0,3))
推荐!!!
2. 最小公倍数(扩展)
LCM:最小公倍数
1 # 对于两个数m,n的lcm: 2 lcm(n,m) = n*m/gcd(n,m)
未完待续...