I am currently using a function that accepts two numbers and uses a loop to find the least common multiple of those numbers,
def lcm[x, y]:
"""This function takes two
integers and returns the L.C.M."""
# Choose the greater number
if x > y:
greater = x
else:
greater = y
while[True]:
if[[greater % x == 0] and [greater % y == 0]]:
lcm = greater
break
greater += 1
return lcm
Is there a built-in module in Python that does it instead of writing a custom function?
asked Aug 6, 2018 at 23:33
0
In Python 3.8 and earlier
There is no such thing built into the stdlib.
However, there is a Greatest Common Divisor function in the math
library. [For Python 3.4 or 2.7, it's buried in fractions
instead.] And writing an LCM on top of a GCD is pretty trivial:
def lcm[a, b]:
return abs[a*b] // math.gcd[a, b]
Or, if you're using NumPy, it's come with an
lcm
function for quite some time now.
Mark Amery
132k78 gold badges394 silver badges442 bronze badges
answered Aug 6, 2018 at 23:39
abarnertabarnert
341k44 gold badges573 silver badges651 bronze badges
2
In Python 3.9+
This is available as math.lcm[]
. It also takes any number of arguments, allowing you to find the lowest common multiple of more than 2 integers.
For example:
>>> from math import lcm
>>> lcm[2, 5, 7]
70
answered Mar 23, 2020 at 22:26
OrangutanOrangutan
9909 silver badges15 bronze badges
Try this instead:
def lcm[x, y]:
from fractions import gcd # or can import gcd from `math` in Python 3
return x * y // gcd[x, y]
answered Aug 6, 2018 at 23:37
Tim PetersTim Peters
64.6k12 gold badges122 silver badges129 bronze badges
5
To simplify your code a little:
def lcm[x, y]:
for currentPossibleLCM in range[max[x,y], [x*y]+1]
if[[currentPossibleLCM % x == 0] and [currentPossibleLCM % y == 0]]:
return currentPossibleLCM
Runtime: O[x*y]
answered Mar 9, 2020 at 22:22
Satbir KiraSatbir Kira
7426 silver badges20 bronze badges
This is not only for two numbers specifically but for finding LCM of an array of integers. [without using math.lcm[]]
import math
from functools import reduce
def lcm[arr]:
l=reduce[lambda x,y:[x*y]//math.gcd[x,y],arr]
return l
answered Dec 26, 2021 at 19:30
In this program, you'll learn to find the LCM of two numbers and display it.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python while Loop
- Python Functions
- Python Function Arguments
- Python User-defined Functions
The least common multiple [L.C.M.] of two numbers is the smallest positive integer that is perfectly divisible by the two given numbers.
For example, the L.C.M. of 12 and 14 is 84.
Program to Compute LCM
# Python Program to find the L.C.M. of two input number
def compute_lcm[x, y]:
# choose the greater number
if x > y:
greater = x
else:
greater = y
while[True]:
if[[greater % x == 0] and [greater % y == 0]]:
lcm = greater
break
greater += 1
return lcm
num1 = 54
num2 = 24
print["The L.C.M. is", compute_lcm[num1, num2]]
Output
The L.C.M. is 216
Note: To test this program, change the values of num1
and num2
.
This program stores two number in num1
and num2
respectively. These numbers are passed to the compute_lcm[]
function. The function returns the L.C.M of two
numbers.
In the function, we first determine the greater of the two numbers since the L.C.M. can only be greater than or equal to the largest number. We then use an infinite while
loop to go from that number and beyond.
In each iteration, we check if both the numbers perfectly divide our number. If so, we store the number as L.C.M. and break from the loop. Otherwise, the number is incremented by 1 and the loop continues.
The above program is slower to run. We can make it more efficient by using the fact that the product of two numbers is equal to the product of the least common multiple and greatest common divisor of those two numbers.
Number1 * Number2 = L.C.M. * G.C.D.
Here is a Python program to implement this.
Program to Compute LCM Using GCD
# Python program to find the L.C.M. of two input number
# This function computes GCD
def compute_gcd[x, y]:
while[y]:
x, y = y, x % y
return x
# This function computes LCM
def compute_lcm[x, y]:
lcm = [x*y]//compute_gcd[x,y]
return lcm
num1 = 54
num2 = 24
print["The L.C.M. is", compute_lcm[num1, num2]]
The output of this program is the same as before. We have two functions compute_gcd[]
and compute_lcm[]
. We require G.C.D. of the numbers to calculate its L.C.M.
So, compute_lcm[]
calls the function compute_gcd[]
to accomplish this.
G.C.D. of two numbers can be calculated efficiently using the Euclidean algorithm.
Click here to learn more about methods to calculate G.C.D in Python.