🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to NumPy Notes
Topic #41

ufunc Finding GCD


Finding GCD (Greatest Common Divisor)

The GCD (Greatest Common Divisor), also known as HCF (Highest Common Factor) is the biggest number that is a common factor of both of the numbers.

Example

  import numpy as np

num1 = 6
num2 = 9

x = np.gcd(num1, num2)

print(x)

Note: Returns: 3 because that is the highest number both numbers can be divided by (6/3=2 and 9/3=3).


Finding GCD in Arrays

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

Note: The reduce() method will use the ufunc, in this case the gcd() function, on each element, and reduce the array by one dimension.

Example

  import numpy as np

arr = np.array([20, 8, 32, 36, 16])

x =
  np.gcd.reduce(arr)

print(x)

Note: Returns: 4 because that is the highest number all values can be divided by.

Want to go beyond the notes?

Join CodingNow 2.0's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

ufunc Finding GCD – FAQs

Quick answers about learning ufunc Finding GCD in NumPy.

This free note from CodingNow 2.0 explains ufunc Finding GCD in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on CodingNow 2.0, including ufunc Finding GCD, is 100% free with no signup required.
With focused practice, most students grasp ufunc Finding GCD in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now