w3resource

Python: Least Common Multiple (LCM) of more than two numbers

Python Basic - 1: Exercise-135 with Solution

Write a Python program that calculates the Least Common Multiple (LCM) of more than two numbers. The numbers should be taken from a given list of positive integers.

From Wikipedia,
In arithmetic and number theory, the least common multiple, lowest common multiple, or smallest common multiple of two integers a and b, usually denoted by lcm(a, b), is the smallest positive integer that is divisible by both a and b. Since division of integers by zero is undefined, this definition has meaning only if a and b are both different from zero. However, some authors define lcm(a,0) as 0 for all a, which is the result of taking the lcm to be the least upper bound in the lattice of divisibility.

Sample Solution-1:

Python Code:

from functools import reduce
def test(nums):
    return reduce(lambda x,y:lcm(x,y),nums)
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
def lcm(a, b):
    return a*b // gcd(a, b)
nums = [ 4, 6, 8 ]
print("Original list elements:")
print(nums)
print("LCM of the numbers of the said array of positive integers: ", test(nums))
nums = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
print("\nOriginal list elements:")
print(nums)
print("LCM of the numbers of the said array of positive integers: ", test(nums))
nums = [ 48, 72, 108  ]
print("\nOriginal list elements:")
print(nums)
print("LCM of the numbers of the said array of positive integers: ", test(nums))

Sample Output:

Original list elements:
[4, 6, 8]
LCM of the numbers of the said array of positive integers:  24

Original list elements:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
LCM of the numbers of the said array of positive integers:  2520

Original list elements:
[48, 72, 108]
LCM of the numbers of the said array of positive integers:  432

Flowchart:

Flowchart: Python - Least Common Multiple (LCM) of more than two numbers.

Sample Solution-2:

Use functools.reduce(), math.gcd() and lcm(x,y) = x * y / gcd(x,y) over the given list.

Python Code:

from functools import reduce
from math import gcd

def lcm(numbers):
  return reduce((lambda x, y: int(x * y / gcd(x, y))), numbers)

print(lcm([ 4, 6, 8 ]))  
print(lcm([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) 
print(lcm([48, 72, 108]))  

Sample Output:

24
2520
432

Flowchart:

Flowchart: Python - Least Common Multiple (LCM) of more than two numbers.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to alternate the case of each letter in a given string and the first letter of the said string must be uppercase.
Next: Write a Python program to reverse all the words which have odd length.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.