Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

ufunc Finding GCD

Finding GCD (Greatest Common Denominator)

The GCD (Greatest Common Divisor), also referred to as HCF (Highest Common Factor), is the largest number that serves as a common factor for both numbers.

Example

Calculate the highest common factor (HCF) of the following two numbers:

import numpy as np

num1 = 6
num2 = 9

x = np.gcd(num1, num2)

print(x)

Finding GCD in Arrays

To determine the highest common factor (HCF) of all values in an array, you can use the reduce() method.

The reduce() method will apply the gcd() function as a universal function (ufunc) to each element, effectively reducing the array by one dimension.

Example

Calculate the greatest common divisor (GCD) for all the numbers in the following array:

import numpy as np

arr = np.array([208323616])

x = np.gcd.reduce(arr)

print(x)