lcm of n numbers python

Solutions on MaxInterview for lcm of n numbers python by the best coders in the world

showing results for - "lcm of n numbers python"
Giulio
09 Jul 2018
1# importing the module
2import math
3
4# function to calculate LCM
5def LCMofArray(a):
6  lcm = a[0]
7  for i in range(1,len(a)):
8    lcm = lcm*a[i]//math.gcd(lcm, a[i])
9  return lcm
10
11
12# array of integers
13arr1 = [1,2,3]
14arr2 = [2,3,4]
15arr3 = [3,4,5]
16arr4 = [2,4,6,8]
17arr5 = [8,4,12,40,26,28]
18
19print("LCM of arr1 elements:", LCMofArray(arr1))
20print("LCM of arr2 elements:", LCMofArray(arr2))
21print("LCM of arr3 elements:", LCMofArray(arr3))
22print("LCM of arr4 elements:", LCMofArray(arr4))
23print("LCM of arr5 elements:", LCMofArray(arr5))
24