當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python numpy.gcd()用法及代碼示例


numpy.gcd(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)該數學函數可幫助用戶計算| arr1 |的GCD值。和| arr2 |元素。

不等於零的兩個或多個數字的最大公約數(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]Input array.

返回: Greatest Common Divisor (GCD) of two or more numbers.

代碼:

# 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]


相關用法


注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.gcd() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。