• 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
  • Ruby
  • 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.  islower Python – Check if All Letters in String Are Lowercase
  • 2.  Convert String to Float with float() in Python
  • 3.  Remove Element from Set in Python
  • 4.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 5.  Create Empty File with Python
  • 6.  pandas nlargest – Find Largest Values in Series or Dataframe
  • 7.  Golden Ratio Constant phi in Python
  • 8.  List of Turtle Shapes in Python
  • 9.  Run Something Every 5 Seconds in Python
  • 10.  Get Current URL with Selenium in 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy