Python Program to Find the LCM of Array Elements

Python Program to Find the LCM of Array Elements

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

  1. Python Program to Compute Compound Interest
  2. Leap Year Program in Python
  3. Python Program to Print Star Pattern
  4. Number Pattern Programs in Python
  5. Python Program to Print Even and Odd numbers From 1 to N
  6. Python Abs() Function: For Absolute Value
  7. How to Check Whether a Number is Fibonacci or Not in Python
  8. Python: Program to Find Power of Number
  9. Python Program to Reverse a Numbers
  10. Python Program to Find Smallest/Minimum of n Numbers
  11. Python Program to Find Largest/Maximum of n Numbers
  12. Python Program to Find The Net Bill Amount After Discount
  13. Python Program to Print Numbers From N to 1 and 1 to N
  14. Python Program to Print Numbers Divisible by 3, 5, 7
  15. Python Program to Print Prime Number 1 to N
  16. How to Find Square of Number in Python
  17. Python Program to Calculate Cube of Number
  18. Python Program to Find LCM of Two Numbers
  19. BMI (Body Mass Index) Calculator in Python
  20. Palindrome Program in Python using while loop, Function, etc
  21. Python: Program to Count Total Number of Bits in Number
  22. Python Random Number Generator Code
  23. Python Program to Calculate n-th term of a Fibonacci Series
  24. Zip Zap Zoom Python Program
  25. Python: program to convert Celsius to Fahrenheit
  26. Python Program to Swap Two Numbers
  27. Python Program to Convert Kilometers to Meters, Miles
  28. Python Program to Find Perfect Number
  29. Python: Program to Find Strong Number
  30. Python Program Create Basic Calculator
  31. Python Program For math.floor() Method
  32. Python Program to Find Roots of Quadratic Equation
  33. Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
  34. Python Program to Check Given Input is Alphabet, Number or Special Character
  35. Python Program to Check IF a Number is Power of Another Number
  36. Python Program to Calculate Area of Triangle
  37. Python Program to Find Area and Circumference of Circle using Radius
  38. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  39. Python Program to Print Binary Value of Numbers From 1 to N

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *