Search code examples
pythonnumpylcm

Finding Lowest Common Multiple using numpy (for more than two inputs)


So I would like to find the lowest common multiple of 4 or more numbers in python. Now I understand that in numpy, you can just use np.lcm but the function is only restricted to two inputs.

import numpy as np
result = np.lcm(12, 8) # calculating the lcm of 12 and 8
print(result)

24

The question is how do I find the lcm of 3 or more integers using the same lcm function in numpy


Solution

  • You'd use np.lcm.reduce(), and pass it an array of numbers:

    >>> np.lcm.reduce([1, 2, 3, 4])
    12