简体   繁体   English

如何找到一系列数字的LCM?

[英]How to find the LCM of a range of numbers?

I want to find the LCM (Least Common Multiple) of the numbers 1 to 20 (1,2,3,4,5,6,7...17,18,19,20).我想找到数字 1 到 20 (1,2,3,4,5,6,7...17,18,19,20) 的 LCM(最小公倍数)。 I have tried using math.lcm() and numpy.lcm() but got the following errors with the following code:我尝试使用 math.lcm() 和 numpy.lcm() 但使用以下代码出现以下错误:

#numpy
import numpy as np

answer = np.lcm(range(1,20))
print(answer)

TypeError: lcm() takes from 2 to 3 positional arguments but 1 were given TypeError: lcm() 从 2 到 3 个位置 arguments 但给出了 1 个

#math.lcm
from math import lcm

answer = lcm(range(1,20))

ImportError: cannot import name 'lcm' from 'math' (/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload/math.cpython-38-darwin.so) ImportError:无法从'math'导入名称'lcm'(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload/math.cpython-38-darwin.so)

If it helps at all, I am using Python 3.8如果它有帮助,我正在使用 Python 3.8

Make sure you are updated to Python 3.9+确保您已更新到 Python 3.9+

I switched to Python 3.10.5 and used:我切换到 Python 3.10.5 并使用:

math.lcm(*range(1,21))数学.lcm(*范围(1,21))

And it worked math.lcm(*range(1,21)) found the Least Common Multiple of the numbers 1-20它起作用math.lcm(*range(1,21))找到了数字 1-20 的最小公倍数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM