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
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下