Calculating Least Common Multiple

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In mathematics, the least common multiple (LCM) is the smallest positive integer that is divisible by two or more numbers without leaving any remainder. For example, the LCM of 4 and 6 is 12, because 12 is the smallest number that is divisible by both 4 and 6.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/variables_data_types -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/numeric_types -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/type_conversion -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/lists -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/tuples -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/function_definition -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/lambda_functions -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/importing_modules -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/using_packages -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/standard_libraries -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/math_random -.-> lab-13682{{"`Calculating Least Common Multiple`"}} python/build_in_functions -.-> lab-13682{{"`Calculating Least Common Multiple`"}} end

Least Common Multiple

Write a function lcm(numbers) that takes a list of numbers as an argument and returns their least common multiple. Your function should use the following formula to calculate the LCM of two numbers x and y: lcm(x, y) = x * y / gcd(x, y), where gcd(x, y) is the greatest common divisor of x and y.

To solve this problem, you can use the functools.reduce() function to apply the lcm() formula to all the numbers in the list. You can also use the math.gcd() function to calculate the greatest common divisor of two numbers.

from functools import reduce
from math import gcd

def lcm(numbers):
  return reduce((lambda x, y: int(x * y / gcd(x, y))), numbers)
lcm([12, 7]) ## 84
lcm([1, 3, 4, 5]) ## 60

Summary

In this challenge, you have learned how to calculate the least common multiple of a list of numbers using the functools.reduce() and math.gcd() functions. The lcm() function uses the lcm(x, y) = x * y / gcd(x, y) formula to calculate the LCM of two numbers.

Other Python Tutorials you may like