LCM of Two Numbers in python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

LCM stands for Least Common Multiple. The lcm of two numbers in python is the smallest positive number or value that can be divided by both the provided numbers. LCM of two numbers (say a and b) is denoted by lcm(a, b).

LCM: Least Common Multiple

LCM stands for Least Common Multiple. The Least Common Multiple or the Lowest Common Multiple comes under arithmetic and number theory. The lcm of two numbers in python is the smallest positive number or value that can be divided by both the provided numbers.

LCM of two numbers (let's say a and b) is denoted by lcm(a, b).

Note:

As we know, when we divide a number by zero, the result is undefined. So, both a and b must not be 0.

For example, the LCM of 15 and 20 is 60; since 60 is the smallest number, it can be divided by both 15 and 20.

There are several ways of calculating the lcm of two numbers in python. We can find LCM of two or more numbers using a loop, gcd method, and in-built functions.

Refer to the image provided below to get a better understanding.

determining lcm of two numbers using hcf

Let us learn about different ways of calculating the lcm of two numbers in python.

Python Program to Compute LCM

Let us code a program to find the LCM of two numbers. Before actually digging into the code, let us first create a flow chart to visualize the working of the code.

python program to compute lcm

Name of the function: lcm(a, b).

First, we will calculate the greater number among a and b. After calculating the greater number among the parameters, we will run an infinite loop. We will divide the greater number by a and b. If both the numbers can divide the greater number completely, we have found our lcm. Otherwise, we will increment the greater number by 11 and perform the same above process until the greater number completely divides the numbers a and b.

Let us calculate the lcm of 4 and 6.

Code:

Output:

Program to Compute LCM Using GCD

Before learning to find lcm in python using gcd or HCF (Highest Common Factor), we should first know what gcd is.

GCD stands for Greatest Common Divisor. The Greatest Common Divisor of two or more numbers is the largest positive integer that divides each number.

We can generate both numbers if we know the lcm and HCF of the two numbers. The basic formula is:

axb=lcm(a,b)gcd(a,b)a x b = lcm(a, b) * gcd(a, b)

So,

LCM(a,b)=(ab)/gcd(a,b)LCM(a, b) = (a * b) / gcd(a, b)

Now, we can calculate the gcd of two or more numbers using recursion and the gcd function of the math module.

Let us see the code implementation for a better understanding:

Output:

Conclusion

  • LCM stands for Least Common Multiple. The lcm of two numbers in python is the smallest positive number or value that can be divided by both the provided numbers.
  • LCM of two numbers (let's say a and b) is denoted by lcm(a, b).
  • There are several ways of calculating the lcm of two numbers in python. We can find LCM of two or more numbers using a loop, gcd method, and in-built functions.

See Also: