Least Common Multiple | Challenge

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-13150{{"`Least Common Multiple | Challenge`"}} python/variables_data_types -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/numeric_types -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/type_conversion -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/lists -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/tuples -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/function_definition -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/lambda_functions -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/importing_modules -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/using_packages -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/standard_libraries -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/math_random -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} python/build_in_functions -.-> lab-13150{{"`Least Common Multiple | Challenge`"}} end

Least Common Multiple Challenge

Problem

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.

Example

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