Skip to content
Related Articles

Related Articles

Improve Article

numpy.gcd() in Python

  • Last Updated : 29 Nov, 2018

numpy.gcd(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) : This mathematical function helps user to calculate GCD value of |arr1| and |arr2| elements.

Greatest Common Divisor (GCD) of two or more numbers, which are not all zero, is the largest positive number that divides each of the numbers.

Example: Finding GCD of 120 and 2250

120 = 2^3 * 3 * 5
2250 = 2 * 3^2 * 5^3

Now, GCD of 120 and 2250 = 2 * 3 * 5 = 30

Parameters :
arr1 / arr2 : [array_like]Input array.

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



Code :




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

Output :

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]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :