Python NumPy LCM – Lowest Common Multiple

least common multiple python: The LCM is the smallest number that is a common multiple of both of the numbers.

The reduce() method can be used to get the Lowest Common Multiple of all values in an array.

The reduce() method will reduce the array by one dimension by using the ufunc, in this example the lcm() function, on each element.

NumPy LCM – Lowest Common Multiple 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 lcm.reduce() function of numpy module to get the lcm of given array elements.
  • Store it in another variable.
  • Print the lcm 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([4, 1, 3, 5, 2])
# Pass the above given array as an argument to the lcm.reduce() function of numpy module
# to get the lcm of given array elements
# Store it in another variable.
rslt_lcm = np.lcm.reduce(gvn_arr)
# Print the lcm of the given array elements
print("The lcm of given array elements = ", rslt_lcm)

Output:

The lcm of given array elements =  60

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 lcm() function to get the LCM of both the given numbers.
  • Print the LCM 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 = 5
# 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 lcm() function
# to get the LCM of both the given numbers
rslt_lcm = np.lcm(fst_numb, scnd_numb)
# Print the LCM of both the given numbers
print("The LCM of both the given numbers = ", rslt_lcm)

Output:

The LCM of both the given numbers =  20

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 lcm.reduce() function of numpy module to get the lcm of given array elements.
  • Store it in another variable.
  • Print the lcm 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 lcm.reduce() function of numpy module
# to get the lcm of given array elements
# Store it in another variable.
rslt_lcm = np.lcm.reduce(gvn_arr)
# Print the lcm of given array elements
print("The lcm of given array elements = ", rslt_lcm)

Output:

Enter some random List Elements separated by spaces = 10 3 5 6
The lcm of given array elements = 30

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 lcm() function to get the LCM of both the given numbers.
  • Print the LCM 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 lcm() function
# to get the LCM of both the given numbers
rslt_lcm = np.lcm(fst_numb, scnd_numb)
# Print the LCM of both the given numbers
print("The LCM of both the given numbers = ", rslt_lcm)

Output:

Enter some random number = 3
Enter some random number = 4
The LCM of both the given numbers = 12