根据欧几里得算法
def gcd(a,b):
while a!=0
a,b = b%a,a
return b
其中 a,b = b%a,a
是通过多重赋值实现值交换
>>> spam,eggs = 42,'hello'
>>> spam
42
>>> eggs
'hello'
>>> spam,eggs = eggs,spam
>>> spam
'hello'
>>> eggs
42
求得,两个数的最大公约数
在shell中测试
>>> def gcd(a,b):
... while a!=0:
... a,b = b%a,a
... return b
...
>>> gcd(24,30)
6
>>>
网友评论