# Python Program to Find LCM

233
views
(Last Updated On: September 2, 2018)

## In this program, you’ll learn how to find Least Common Multiple or find LCM

To properly understand this example of how to find LCM in Python, you should have the knowledge of following Python programming topics:

1. Python while Loop
2. Python Functions
3. Python Function Arguments
4. Python User-defined Functions

Before we start with code lets first understand what is Least Common Multiple (LCM).

A common multiple is a number that is a multiple of two or more numbers. The common multiples of 3 and 4 are 0, 12, 24, ….

The least common multiple (LCM) of two numbers is the smallest number (not zero) that is a multiple of both.

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 find the L.C.M. of two input number

[sourcecode language=”python”]
# define a function
def lcm(x, y):
&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;This function takes two
integers and returns the L.C.M.&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;

# choose the greater number
if x &amp;amp;gt; y:
greater = x
else:
greater = y

while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1

return lcm

# change the values of num1 and num2 for a different result
num1 = 54
num2 = 24

# uncomment the following lines to take input from the user
#num1 = int(input(&amp;amp;quot;Enter first number: &amp;amp;quot;))
#num2 = int(input(&amp;amp;quot;Enter second number: &amp;amp;quot;))

print(&amp;amp;quot;The L.C.M. of&amp;amp;quot;, num1,&amp;amp;quot;and&amp;amp;quot;, num2,&amp;amp;quot;is&amp;amp;quot;, lcm(num1, num2))[/sourcecode]

Output:

[sourcecode language=”plain”]The L.C.M. of 54 and 24 is 216[/sourcecode]

Note: To test this program, you’ll have to change the values of `num1` and `num2`.

The above program stores two number in `num1` and `num2` respectively, and then it passes them to a function which returns the L.C.M.

In the function, we have first determined the greater of the two number since the L.C.M. can only be greater than or equal to the largest number. Then we use an infinite `while` loop to go from that number and beyond.

In each of the iteration, we check if both the input 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 program above 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 least common multiple and greatest common divisors of those two numbers.

#### Program to find the L.C.M. of two input number

[sourcecode language=”plain”]
# define gcd function
def gcd(x, y):
&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;This function implements the Euclidian algorithm
to find G.C.D. of two numbers&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;

while(y):
x, y = y, x % y

return x

# define lcm function
def lcm(x, y):
&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;This function takes two
integers and returns the L.C.M.&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;

lcm = (x*y)//gcd(x,y)
return lcm

# change the values of num1 and num2 for a different result
num1 = 54
num2 = 24

# uncomment the following lines to take input from the user
#num1 = int(input(&amp;amp;quot;Enter first number: &amp;amp;quot;))
#num2 = int(input(&amp;amp;quot;Enter second number: &amp;amp;quot;))

print(&amp;amp;quot;The L.C.M. of&amp;amp;quot;, num1,&amp;amp;quot;and&amp;amp;quot;, num2,&amp;amp;quot;is&amp;amp;quot;, lcm(num1, num2))[/sourcecode]

Output:

[sourcecode language=”plain”]The L.C.M. of 54 and 24 is 216[/sourcecode]

Note: Here to test this program, change the values of `num1` and `num2`.

We have two functions `gcd()` and `lcm()`. We require G.C.D. of the numbers to calculate its L.C.M.

So, `lcm()` calls the function `gcd()` to accomplish this. G.C.D. of two numbers can be calculated efficiently using the `Euclidean algorithm`.

Ask your questions and clarify your/others doubts on How to find lcm in Python by commenting. Python Documentation