参考博客:
https://blog.csdn.net/jackfjw/article/details/82469300
代码:
(1)比较粗暴的方法
nums = [18, 15, 30]
#最大公约数
def hcf(nums):
min_val = min(nums)
res = 0
if min_val == 1:
return 1
for i in range(2, min_val + 1):
if sum(list(map(lambda x : x%i, nums))) == 0:
res = i
break
return res
#最小公倍数
def lcm(nums):
gen = max(nums)
while(True):
if sum(list(map(lambda x : gen%x, nums))) == 0:
lcm = gen
break
gen += 1
return lcm
(2)最小公倍数一般方法:参考博客中有更多的方法
#最大公约数
def hcf(a,b):
x = a % b
while (x != 0):
a = b
b = x
x = a % b
return b