Python NumPy GCD – Greatest Common Denominator

Greatest common denominator python: The Greatest Common Denominator(GCD), also known as the HCF (Highest Common Factor), is the largest number that is also a common factor of both numbers.

The reduce() method can be used to find the Highest Common Factor of all values in an array.
The reduce() method will reduce the array by one dimension by using the ufunc, in this example the gcd() function, on each element.

NumPy GCD – Greatest Common Denominator in Python

Method #1: Using Built-in Functions (Static Input)

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array. Store it in a variable.
  • Pass the above-given array as an argument to the gcd.reduce() function of numpy module to get the GCD of given array elements.
  • Store it in another variable.
  • Print the GCD of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to create an array.
# Store it in a variable.
gvn_arr = np.array([20, 10, 5, 2])
# Pass the above given array as an argument to the gcd.reduce() function of numpy module
# to get the GCD of given array elements
# Store it in another variable.
rslt_gcd = np.gcd.reduce(gvn_arr)
# Print the GCD of the given array elements
print("The GCD of given array elements = ", rslt_gcd)

Output:

The GCD of given array elements = 1

Example2

Approach:

  • Import numpy module using the import keyword.
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the above given first and second numbers as arguments to the gcd() function to get the GCD of both the given numbers.
  • Print the GCD of both the given numbers.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the first number as static input and store it in a variable.
fst_numb = 6
# Give the second number as static input and store it in another variable.
scnd_numb = 20
# Pass the above given first and second numbers as arguments to the gcd() function
# to get the GCD of both the given numbers
rslt_lcm = np.gcd(fst_numb, scnd_numb)
# Print the GCD of both the given numbers
print("The GCD of both the given numbers = ", rslt_lcm)

Output:

The GCD of both the given numbers =  2

Method #2: Using Built-in Functions (User Input)

Example1

Approach:

  • Import numpy module using the import keyword.
  • Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
  • Pass the above list as an argument to the array() function to create an array. Store it in a variable.
  • Pass the above-given array as an argument to the gcd.reduce() function of numpy module to get the GCD of given array elements.
  • Store it in another variable.
  • Print the GCD of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the list as user input using the list(),map(),split(),int 
# functions and store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the above list as an argument to the array() function to create an array.
# Store it in a variable.
gvn_arr = np.array(gvn_lst)
# Pass the above array as an argument to the gcd.reduce() function of numpy module
# to get the GCD of given array elements
# Store it in another variable.
rslt_gcd = np.gcd.reduce(gvn_arr)
# Print the GCD of given array elements
print("The GCD of given array elements = ", rslt_gcd)

Output:

Enter some random List Elements separated by spaces = 12 4 24 18
The GCD of given array elements = 2

Example2

Approach:

  • Import numpy module using the import keyword.
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the above given first and second numbers as arguments to the gcd() function to get the GCD of both the given numbers.
  • Print the GCD of both the given numbers.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the first number as user input using the int(input()) function 
# and store it in a variable.
fst_numb = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) function 
# and store it in another variable.
scnd_numb = int(input("Enter some random number = "))
# Pass the above given first and second numbers as arguments to the gcd() function
# to get the GCD of both the given numbers
rslt = np.gcd(fst_numb, scnd_numb)
# Print the GCD of both the given numbers
print("The GCD of both the given numbers = ", rslt)

Output:

Enter some random number = 30
Enter some random number = 15
The GCD of both the given numbers = 15