Python :: GCD
:information_source: math.gcd 仅适用于两个数字
计算两个或多个数字/列表之间的最大公约数。
在 helperGcdfunction 使用递归。基本情况是 y equals 0。在这种情况下,请返回 x。否则,返回的GCD y 和除法的其余部分 x/y。
使用内置模块中的reduce函数 functools。还spread 为javascript定义了一种方法 ,例如传播列表。
from functools import reduce
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def gcd(*args):
numbers = []
numbers.extend(spread(list(args)))
def _gcd(x, y):
return x if not y else gcd(y, x % y)
return reduce((lambda x, y: _gcd(x, y)), numbers)gcd(8,36) #4
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短