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. |
Calculate the lowest common multiple (LCM) of the values in the following array:
import numpy as np arr = np.array([3, 6, 9]) 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). |
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(1, 11) x = np.lcm.reduce(arr) print(x) |