在 NumPy 中返回最大公約數和最小公倍數
如需返回最大公約數,請在 Python Numpy 中使用 numpy.gcd() 方法。引數是數值陣列。如果 x1.shape != x2.shape,它們必須可廣播到通用形狀(該形狀成為輸出的形狀)。
如需返回最小公倍數,請在 Python Numpy 中使用 numpy.lcm() 方法。絕對值輸入的最大公約數,如果 x1 和 x2 都是標量,則這是一個標量。
步驟
首先,匯入所需的庫 −
import numpy as np
如需返回最大公約數,請在 Python Numpy 中使用 numpy.gcd() 方法。引數是數值陣列。如果 x1.shape != x2.shape,它們必須可廣播到通用形狀(該形狀成為輸出的形狀) −
print("LCM...
", np.lcm(15, 30))
print("LCM...
", np.lcm(10, 50))
print("LCM...
", np.lcm.reduce([6, 18, 30]))如需返回最小公倍數,請在 Python Numpy 中使用 numpy.lcm() 方法 −
print("GCD...
", np.gcd(15, 30))
print("GCD...
", np.gcd(10, 50))
print("GCD...
", np.gcd.reduce([25, 75, 100, 125]))示例
import numpy as np
# To returns the greatest common divisor, use the numpy.gcd() method in Python Numpy
# The parameters are arrays of values.
# If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).
print("LCM...
", np.lcm(15, 30))
print("LCM...
", np.lcm(10, 50))
print("LCM...
", np.lcm.reduce([6, 18, 30]))
# To returns the lowest common multiple, use the numpy.lcm() method in Python Numpy
print("GCD...
", np.gcd(15, 30))
print("GCD...
", np.gcd(10, 50))
print("GCD...
", np.gcd.reduce([25, 75, 100, 125]))輸出
LCM... 30 LCM... 50 LCM... 90 GCD... 15 GCD... 10 GCD... 25
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP