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.
Given an array of n numbers, find LCM of it.
Input : {1, 2, 8, 3} Output : 24 Input : {2, 7, 3, 9, 4} Output : 252
We know,
The above relation only holds for two numbers,
The idea here is to extend our relation for more than 2 numbers. Let’s say we have an array arr[] that contains n elements whose LCM needed to be calculated.
The main steps of our algorithm are:
- Initialize ans = arr[0].
- Iterate over all
the elements of the array i.e. from i = 1 to i = n-1
At the ith iteration ans = LCM[arr[0], arr[1], …….., arr[i-1]]. This can be done easily as LCM[arr[0], arr[1], …., arr[i]] = LCM[ans, arr[i]]. Thus at i’th iteration we just have to do ans = LCM[ans, arr[i]] = ans x arr[i] / gcd[ans, arr[i]]
Below is the implementation of above algorithm :
C++
#include
using
namespace
std;
typedef
long
long
int
ll;
int
gcd[
int
a,
int
b]
{
if
[b == 0]
return
a;
return
gcd[b, a % b];
}
ll findlcm[
int
arr[],
int
n]
{
ll ans = arr[0];
for
[
int
i = 1; i < n; i++]
ans = [[[arr[i] * ans]] /
[gcd[arr[i], ans]]];
return
ans;
}
int
main[]
{
int
arr[] = { 2, 7, 3, 9, 4 };
int
n =
sizeof
[arr] /
sizeof
[arr[0]];
printf
[
"%lld"
, findlcm[arr, n]];
return
0;
}
Java
public
class
GFG {
public
static
long
lcm_of_array_elements[
int
[] element_array]
{
long
lcm_of_array_elements =
1
;
int
divisor =
2
;
while
[
true
] {
int
counter =
0
;
boolean
divisible =
false
;
for
[
int
i =
0
; i < element_array.length; i++] {
if
[element_array[i] ==
0
] {
return
0
;
}
else
if
[element_array[i] <
0
] {
element_array[i] = element_array[i] * [-
1
];
}
if
[element_array[i] ==
1
] {
counter++;
}
if
[element_array[i] % divisor ==
0
] {
divisible =
true
;
element_array[i] = element_array[i] / divisor;
}
}
if
[divisible] {
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else
{
divisor++;
}
if
[counter == element_array.length] {
return
lcm_of_array_elements;
}
}
}
public
static
void
main[String[] args]
{
int
[] element_array = {
2
,
7
,
3
,
9
,
4
};
System.out.println[lcm_of_array_elements[element_array]];
}
}
Python
def
find_lcm[num1, num2]:
if
[num1>num2]:
num
=
num1
den
=
num2
else
:
num
=
num2
den
=
num1
rem
=
num
%
den
while
[rem !
=
0
]:
num
=
den
den
=
rem
rem
=
num
%
den
gcd
=
den
lcm
=
int
[
int
[num1
*
num2]
/
int
[gcd]]
return
lcm
l
=
[
2
,
7
,
3
,
9
,
4
]
num1
=
l[
0
]
num2
=
l[
1
]
lcm
=
find_lcm[num1, num2]
for
i
in
range
[
2
,
len
[l]]:
lcm
=
find_lcm[lcm, l[i]]
print
[lcm]
C#
using
System;
public
class
GFG {
public
static
long
lcm_of_array_elements[
int
[] element_array]
{
long
lcm_of_array_elements = 1;
int
divisor = 2;
while
[
true
] {
int
counter = 0;
bool
divisible =
false
;
for
[
int
i = 0; i < element_array.Length; i++] {
if
[element_array[i] == 0] {
return
0;
}
else
if
[element_array[i] < 0] {
element_array[i] = element_array[i] * [-1];
}
if
[element_array[i] == 1] {
counter++;
}
if
[element_array[i] % divisor == 0] {
divisible =
true
;
element_array[i] = element_array[i] / divisor;
}
}
if
[divisible] {
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else
{
divisor++;
}
if
[counter == element_array.Length] {
return
lcm_of_array_elements;
}
}
}
public
static
void
Main[]
{
int
[] element_array = { 2, 7, 3, 9, 4 };
Console.Write[lcm_of_array_elements[element_array]];
}
}
PHP
Javascript
function
gcd[a, b]
{
if
[b == 0]
return
a;
return
gcd[b, a % b];
}
function
findlcm[arr, n]
{
let ans = arr[0];
for
[let i = 1; i < n; i++]
ans = [[[arr[i] * ans]] /
[gcd[arr[i], ans]]];
return
ans;
}
let arr = [ 2, 7, 3, 9, 4 ];
let n = arr.length;
document.write[findlcm[arr, n]];
Time Complexity: O[n * log[min[a, b]]], where n represents the size of the given array.
Auxiliary Space: O[n*log[min[a, b]]] due to recursive stack space.
Below is implementation of above algorithm Recursively :
C++
#include
using
namespace
std;
int
LcmOfArray[vector arr,
int
idx]{
if
[idx == arr.size[]-1]{
return
arr[idx];
}
int
a = arr[idx];
int
b = LcmOfArray[arr, idx+1];
return
[a*b/__gcd[a,b]];
}
int
main[] {
vector arr = {1,2,8,3};
cout