Answers for "python lcm of list"

1

lcm python

# Python program to find the L.C.M. of two input number

# This function computes GCD 
def compute_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# This function computes LCM
def compute_lcm(x, y):
   lcm = (x*y)//compute_gcd(x,y)
   return lcm

num1 = 54
num2 = 24 

print("The L.C.M. is", compute_lcm(num1, num2))
Posted by: Guest on December-25-2020
1

lcm of n numbers python

# importing the 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]
arr2 = [2,3,4]
arr3 = [3,4,5]
arr4 = [2,4,6,8]
arr5 = [8,4,12,40,26,28]

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))
Posted by: Guest on December-11-2020

Python Answers by Framework

Browse Popular Code Answers by Language