# NumPy Ufunc LCM – Least Common Multiple

Numpy Ufunc Lcm can be used to calculate the least common multiple of two or more integers.

The least common multiple (LCM) of two or more integers is the smallest positive integer that is divisible by all of them.

For example:

The LCM of 4 and 6 is 12, since 12 is the smallest integer that is divisible by both 4 and 6.

In this article, we will examine NumPy LCM function, how it is implemented, and some examples of how it can be utilized in practice.

## Numpy Ufunc Lcm Use Cases

The NumPy Ufunc lcm can be useful in scientific computing and data analysis applications where numerical computations involving integers are common.

It has several useful features:

• np.lcm() can handle inputs of various types, including integers, floating-point numbers, and complex numbers.
• np.lcm() supports broadcasting, which allows you to apply the function to arrays of different shapes and sizes.
• It is implemented in compiled C code, which makes it much faster than equivalent Python code for large arrays.
• The np.lcm() function also supports higher-order LCMs, which can be useful in applications that require multiple levels of LCM calculation.

## Find Least Common Multiple

In Numpy Ufunc lcm, the Lowest Common Multiple is the smallest number that is the common multiple of two numbers.

In the following example, compute the LCM of two numbers:

#### Example:

import numpy as npy mrx1 = 7 mrx2 = 10 ample = npy.lcm(mrx1, mrx2) print(ample)
Outcome: 70 Due to the fact that there is only one lowest common multiple of both numbers (7*10=70 and 10*7=70).

Calculate the LCM of 5 and 9 in the below example:

#### Example:

import numpy as npy mrx1 = 5 mrx2 = 9 ample = npy.lcm(mrx1, mrx2) print(ample)
Outcome: 45 Due to the fact that there is only one lowest common multiple of both numbers (5*9=45 and 9*5=45).

## LCM in Arrays

You can retrieve the Lowest Common Multiple in an array by calling the reduce() method in Numpy Ufunc LCM.

In this case, the reduce() method will invoke a ufunc that will shrink the array by one dimension by calling the lcm() function for each item of the array.

Utilizing the factorial_arr array, calculate the LCM of its items:

#### Example:

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24]) mrx = npy.lcm.reduce(factorial_arr) print(mrx)
Outcome: 24 Due to the fact that there is only one lowest common multiple of all four numbers (1*24=24, 2*12 = 24, 6*4 = 24 and 24*1=24).

Check out the LCM of the square_arr array:

#### Example:

import numpy as npy square_arr = npy.array([1, 4, 8, 16, 25]) mrx = npy.lcm.reduce(square_arr) print(mrx)
Outcome: 400 Due to the fact that there is only one lowest common multiple of all five numbers (1*400=400, 4*100 = 400, 8*50 = 400, 16*25 = 400 and 25*16=400).

Compute the LCM of the num_arr array whose number range is 10 to 16.

#### Example:

import numpy as npy num_arr = npy.arange(10, 16) mrx = npy.lcm.reduce(num_arr) print(mrx)

Initialize the range of the below array from 2 to 5 then implement the reduce() function:

#### Example:

import numpy as npy num_arr = npy.arange(2, 5) mrx = npy.lcm.reduce(num_arr) print(mrx)

### Example Explanation

In above example First we have created an array of numbers using the numpy.arange() function. In this case, the array contains the numbers 2, 3, and 4, which are the numbers for which we want to find the LCM.

Next, we have used the reduce method is called on the numpy.lcm function, passing in the array of numbers as its argument.

The reduce method iteratively applies the function to the elements of the array until a single result is obtained.

In this case, the numpy.lcm function is applied to the first two elements of the array, then the result is combined with the third element using the numpy.lcm function again.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0 