• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Find Least Common Multiple of Numbers Using Python

Find Least Common Multiple of Numbers Using Python

May 8, 2022 Leave a Comment

To find the least common multiple of two numbers, the easiest way is to use the equation that the product of two numbers equals the least common multiple times the greatest common divisor.

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

def lcm(x, y):
    return x * y / gcd(x, y)

print(lcm(20,37))
print(lcm(4,6))

#Output:
740.0
12.0

You can also use a loop which will brute force the least common multiple of two or more numbers.

def lcm(lst):
    lcm_temp = max(lst)
    while(True):
        if all(lcm_temp % x == 0 for x in lst):
            break
        lcm_temp = lcm_temp + 1
    return lcm_temp

print(lcm([20,37]))
print(lcm([4,6]))
print(lcm([20,16,28]))
print(lcm([6,8,10,12]))

#Output:
740
12
560
120

Python allows us to implement complex algorithms to do various calculations. One such calculation is finding the least common multiple of two numbers

The least common multiple of two integers a and b is the smallest positive integer that is divisible by both.

To find the least common multiple of two numbers, the easiest way is to use the mathematical identity that states that the product of two numbers is equal to the product of the greatest common divisor (GCD) and the least common multiple (LCM).

Therefore, we can get the GCD of two numbers with the Euclidean Algorithm and then just divide to get the least common multiple.

Below is a function which will calculate the least common multiple of two integers in Python.

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

def lcm(x, y):
    return x * y / gcd(x, y)

print(lcm(20,37))
print(lcm(4,6))

#Output:
740.0
12.0

How to Calculate the LCM of Multiple Numbers in Python

If you want to calculate the LCM of more than two numbers, you can use a brute force method which will loop until you’ve found a number which each number can divide evenly.

First, we take the max of a list of integers to get the starting point.

Then, we loop until we find a number that each element in the list of numbers divides evenly.

When we find a number which is divisible by all of the numbers in the list, we have found our least common multiple.

Below is the function which will find the least common multiple given a list of integers in Python.

def lcm(lst):
    lcm_temp = max(lst)
    while(True):
        if all(lcm_temp % x == 0 for x in lst):
            break
        lcm_temp = lcm_temp + 1
    return lcm_temp

print(lcm([20,37]))
print(lcm([4,6]))
print(lcm([20,16,28]))

#Output:
740
12
560

Hopefully this article has been useful for you to learn how to get the least common multiple of two or more numbers using Python.

Other Articles You'll Also Like:

  • 1.  Truncate Decimal from Number with Python math.trunc() Function
  • 2.  Using Selenium to Check if Element Exists in Python
  • 3.  Python Check if Attribute Exists in Object with hasattr() Function
  • 4.  Creating a Random Color Turtle in Python
  • 5.  Remove Extension from Filename in Python
  • 6.  Using Python to Reverse Tuple
  • 7.  Calculating Sum of Squares in Python
  • 8.  Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable
  • 9.  Using Python to Add Trailing Zeros to String
  • 10.  Check if Word is Palindrome Using Recursion with Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy