NumPy GCD Greatest Common Divisor
🔢 NumPy GCD (Greatest Common Divisor)
NumPy provides a vectorized function np.gcd() to compute the greatest common divisor of integers element-wise.
✅ 1. Basic GCD of Two Numbers
-
GCD of 12 and 15 is 3 → largest number that divides both
✅ 2. GCD of Arrays
-
Computes element-wise GCD
-
gcd_arr[i] = GCD(arr1[i], arr2[i])
✅ 3. GCD with Scalars and Arrays
-
Scalar is broadcasted to array automatically
✅ 4. GCD of Multiple Numbers
-
NumPy
gcdis element-wise. To find GCD of multiple numbers, usereduce:
-
Computes GCD of all elements in the array
✅ 5. Notes & Tips
-
GCD is always ≤ min(a, b)
-
Useful in fractions simplification, ratio calculations, and number theory problems
-
Works with integers only
🎯 Practice Exercises
-
Find the GCD of 36 and 60.
-
Find element-wise GCD of arrays
[12, 18, 24]and[6, 9, 12]. -
Find the GCD of
[8, 12, 16, 20]usingnp.gcd.reduce().
