Python program to find lcm of n numbers or array elements; In this tutorial, you will learn how to find the LCM (lowest common multiple) of the elements of the array or n numbers in python.
LCM is the lowest multiple of two or more numbers. Multiples of a number are those numbers which when divided by the number leave no remainder.
Python Program to Find the LCM of the Array Elements or n Numbers
Let’s use the following algorithm to write a program to find lcm of given array elements in python:
- Algorithm to find the LCM of array elements
- Python program to find lcm of array elements
Algorithm to find the LCM of array elements
- Import the math module to find the GCD of two numbers using math.gcd() function.
- At first, find the LCM of initial two numbers using: LCM(a,b) = a*b/GCD(a,b).
- And, then find the LCM of three numbers with the help of LCM of first two numbers using LCM(ab,c) = lcm(lcm(a1, a2), a3). The same concept we have implemented.
Python program to find lcm of array elements
# Python program to find the LCM of the array elements
# import math module
import math
# function to calculate LCM
def LCMofArray(a):
lcm = a[0]
for i in range(1,len(a)):
lcm = lcm*a[i]//math.gcd(lcm, a[i])
return lcm
# array of integers
arr1 = [1,2,3,4]
arr2 = [2,3,4,5]
arr3 = [3,4,5,6]
arr4 = [2,4,6,8,10]
arr5 = [8,4,12,40,26,28,30]
print("LCM of arr1 elements:", LCMofArray(arr1))
print("LCM of arr2 elements:", LCMofArray(arr2))
print("LCM of arr3 elements:", LCMofArray(arr3))
print("LCM of arr4 elements:", LCMofArray(arr4))
print("LCM of arr5 elements:", LCMofArray(arr5))
Output
LCM of arr1 elements: 12 LCM of arr2 elements: 60 LCM of arr3 elements: 60 LCM of arr4 elements: 120 LCM of arr5 elements: 10920
Recommended Python Programs
- Python Program to Compute Compound Interest
- Leap Year Program in Python
- Python Program to Print Star Pattern
- Number Pattern Programs in Python
- Python Program to Print Even and Odd numbers From 1 to N
- Python Abs() Function: For Absolute Value
- How to Check Whether a Number is Fibonacci or Not in Python
- Python: Program to Find Power of Number
- Python Program to Reverse a Numbers
- Python Program to Find Smallest/Minimum of n Numbers
- Python Program to Find Largest/Maximum of n Numbers
- Python Program to Find The Net Bill Amount After Discount
- Python Program to Print Numbers From N to 1 and 1 to N
- Python Program to Print Numbers Divisible by 3, 5, 7
- Python Program to Print Prime Number 1 to N
- How to Find Square of Number in Python
- Python Program to Calculate Cube of Number
- Python Program to Find LCM of Two Numbers
- BMI (Body Mass Index) Calculator in Python
- Palindrome Program in Python using while loop, Function, etc
- Python: Program to Count Total Number of Bits in Number
- Python Random Number Generator Code
- Python Program to Calculate n-th term of a Fibonacci Series
- Zip Zap Zoom Python Program
- Python: program to convert Celsius to Fahrenheit
- Python Program to Swap Two Numbers
- Python Program to Convert Kilometers to Meters, Miles
- Python Program to Find Perfect Number
- Python: Program to Find Strong Number
- Python Program Create Basic Calculator
- Python Program For math.floor() Method
- Python Program to Find Roots of Quadratic Equation
- Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
- Python Program to Check Given Input is Alphabet, Number or Special Character
- Python Program to Check IF a Number is Power of Another Number
- Python Program to Calculate Area of Triangle
- Python Program to Find Area and Circumference of Circle using Radius
- Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
- Python Program to Print Binary Value of Numbers From 1 to N