Python numpy.gcd()
numpy.gcd(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) :这个数学函数可以帮助用户计算出arr1和arr2元素的GCD值。
两个或多个不全是零的数字的最大公除数(GCD)是除以每个数字的最大正数。
示例:寻找120和2250的GCD
120 = 2^3 * 3 * 5
2250 = 2 * 3^2 * 5^3
Now, GCD of 120 and 2250 = 2 * 3 * 5 = 30
参数 :
arr1 / arr2 :[array_like]输入阵列。
返回 :两个或多个数字的最大公除数(GCD)。
代码 :
# Python program illustrating
# gcd() method
import numpy as np
arr1 = [120, 24, 42, 10]
arr2 = [2250, 12, 20, 50]
print ("arr1 : ", arr1)
print ("arr2 : ", arr2)
print ("\nGCD of arr1 and arr2 : ", np.gcd(arr1, arr2))
print ("\nGCD of arr1 and 10 : ", np.gcd(arr1, 10))
输出 :
arr1 : [120, 24, 42, 10]
arr2 : [2250, 12, 20, 50]
GCD of arr1 and arr2 : [30, 12, 2, 10]
GCD of arr1 and 10 : [10, 2, 2, 10]