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).

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more