Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

NumPy LCM 最小公倍数


求最小公倍数 (LCM)

最小公倍数是指两个数的公倍数中最小的一个。

示例

求以下两个数的最小公倍数

import numpy as np

num1 = 4
num2 = 6

x = np.lcm(num1, num2)

print(x)
自己试试 »

返回值: 12,因为这是这两个数的最小公倍数 (4*3=12 且 6*2=12)。


在数组中求最小公倍数

要查找数组中所有值的最小公倍数,可以使用 reduce() 方法。

reduce() 方法将对每个元素使用 ufunc(在本例中为 lcm() 函数),并将数组的维度减少一个。

示例

求以下数组中所有值的最小公倍数

import numpy as np

arr = np.array([3, 6, 9])

x = np.lcm.reduce(arr)

print(x)
自己试试 »

返回值: 18,因为这是所有三个数的最小公倍数 (3*6=18, 6*3=18 且 9*2=18)。

示例

求数组中所有值的最小公倍数,其中数组包含从 1 到 10 的所有整数

import numpy as np

arr = np.arange(1, 11)

x = np.lcm.reduce(arr)

print(x)
自己试试 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.