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.
This python program allows the user to enter two positive integer values and compute GCD using while loop. Next, python program calculate LCM of two positive integer values using GCD. In Mathematics, the Greatest Common Divisor [GCD] of two or more integers is the largest positive integer that
divides given integer values. For example, the GCD value of integer 8 and 12 is 4 because both 8 and 12 are divisible by 1, 2, and 4 [the remainder is 0], and the largest positive integer among them is 4.Write a Python program to Compute the greatest common divisor [GCD] and least common multiple [LCM] of two integer
What is Greatest common Divisior [GCD]?
The Greatest Common Divisor [GCD] is also known as Highest Common Factor [HCF], or Greatest Common Factor [GCF], or Highest Common Divisor [HCD], or Greatest Common Measure [GCM].
What is Least Common Multiple[LCM]?
In Mathematics, the Least Common Multiple [LCM] of two or more integers is the smallest positive integer that is totally divisible by the given integer values.Remainder should be zero after division. For example, the LCM value of integer 2 and 3 is 12 because 12 is the smallest positive integer that is divisible by both 2 and 3 [the remainder is 0].
The least common multiple is also known as lowest common multiple, or smallest common multiple of two integers.
Below is source code
# python program to find LCM of two number using GCD #input two numbers n1 = int[input["Enter First number :"]] n2 = int[input["Enter Second number :"]] x = n1 y = n2 while[n2!=0]: t = n2 n2 = n1 % n2 n1 = t gcd = n1 print["GCD of {0} and {1} = {2}".format[x,y,gcd]] lcm = [x*y]/gcd print["LCM of {0} and {1} = {2}".format[x,y,lcm]]
Output:
>> %Run gcdlcm.py Enter First number :54 Enter Second number :24 GCD of 54 and 24 = 6 LCM of 54 and 24 = 216.0 >>> %Run gcdlcm.py Enter First number :4 Enter Second number :6 GCD of 4 and 6 = 2 LCM of 4 and 6 = 12.0 >>> %Run gcdlcm.py Enter First number :125 Enter Second number :25 GCD of 125 and 25 = 25 LCM of 125 and 25 = 125.0
Below is Snapshot of python program
Explaination:
This python program allows the user to enter two positive integer values n1 and n2. We declared two variables x and y and assigned value of n1 and n2 to them. We used while loop to check the remainder of n1 % n2 and n2 is equals to zero or not. If true, n1 is calculated. After that, value of n1 is assigned to GCD. With the help of GCD, we can calculate LCM of two integer. Here We used mathematical formula to calculate LCM.
First,we multiplied two positive integers and then divided by gcd to compute LCM of two integer.