لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy ufunc GCD Or HCF Greatest Common Denominator

Are GCD And HCF Are Same?

GCD (Greatest Common Denominator), is also known as HCF (Highest Common Factor)

How to find GCD/HCF Greatest Common Denominator?

The GCD (Greatest Common Denominator), also known as HCF (Highest Common Factor) is the biggest number that is a common factor of both of the numbers. In NumPy ufunc we use the following to find GCD Or HCF

x = np.gcd(num1, num2)

Example 1: Find the HCF of the following two numbers.

Code

import numpy as np

num1 = 5
num2 = 10

x = np.gcd(num1, num2)

print(x)

the output will be

5

Returns: 5 because that is the highest number both numbers can be divided by (5/5=1 and 10/5=2).

Finding GCD in Arrays

To find the Highest Common Factor of all values in an array, we can use the reduce() method.

The reduce() method in ufunc, which in this case is the gcd() function will be used on each element to reduce the array by one dimension.

Example 2: Find the GCD for all of the numbers in following array.

Code

import numpy as np

arr = np.array([10, 4, 16, 18, 8])

x = np.gcd.reduce(arr)

print(x)

the output will be

2

Returns: 2 because that is the highest number all values can be divided by.
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.