Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Finding LCM in Arrays

To determine the lowest common multiple (LCM) of all values in an array, you can utilize the reduce() method.

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

Example

Calculate the lowest common multiple (LCM) of the values in the following array:

import numpy as np

arr = np.array([369])

x = np.lcm.reduce(arr)

print(x)

Returns: 18, as it is the lowest common multiple of all three numbers (3 × 6 = 18, 6 × 3 = 18, and 9 × 2 = 18).

Example

Calculate the lowest common multiple (LCM) of all the integers in an array containing values from 1 to 10:

import numpy as np

arr = np.arange(111)

x = np.lcm.reduce(arr)

print(x)