codingstreets
Search
Close this search box.
python-numpy-lcm

Introduction to Python NumPy LCM

In This Article, You Will Learn About Python Numpy LCM.

Python Numpy LCM – Before moving ahead, let’s know a bit of Python Numpy Differences

LCM – Lowest Common Multiple

The Lowest Common Multiple is the lowest common multiple number that is common in multiple of both numbers.

 Example – Finding the LCM of both numbers.

import numpy as np

num1 = 6     
num2 = 3     

x = np.lcm(num1, num2)

print(x)
Output -

6

Because 6 is the only lowest common multiple value that is in both 3 and 6 Table. 

6*1 = 6
3*2 = 6

As shown above, it returned six as LCM of given numbers.

LCM in Arrays

Using reduce() method to find the Lowest Common Multiple of all values of an array.

The reduce() technique will use the ufunc with the lcm() function to on each element to reduce the array by one size.

Example -Finding LCM of the values of the array.

import numpy as np

arr = np.array([2, 3, 6])        

x = np.lcm.reduce(arr)

print(x)
Output -

6

Because 6 is the only lowest common multiple value that is present in 2, 3, and 6 Table. 

2*3=6    3*2=6    6*1=6

As a result, it returned six as LCM of given numbers.

Example – Finding the LCM of the all integers from 1 to 10.

import numpy as np

arr = np.arange(1, 11)

x = np.lcm.reduce(arr)

print(x)
python-numpy-lcm

As a result, it returned 2520 as LCM of the given range.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on