Given three integers x, y, z, the task is to compute the value of GCD(LCM(x,y), LCM(x,z)) where, GCD = Greatest Common Divisor, LCM = Least Common Multiple
Examples:
Input: x = 15, y = 20, z = 100
Output: 60
Explanation: The GCD of 15 and 20 is 5, and the LCM of 15 and 20 is 60, which is then multiplied by the given z (100) to illustrate the distributive property in LCM and GCD operations
Input: x = 30, y = 40, z = 400
Output: 120
Explanation: The GCD of 30 and 40 is 10, and the LCM of 30 and 40 is 120, which is then multiplied by the given z (400) to show the distributive property in combining GCD and LCM operations
[Naive Approach] Using GCD and LCM Separately
One way to solve it is by finding GCD(x, y), and using it we find LCM(x, y). Similarly, we find LCM(x, z) and then we finally find the GCD of the obtained results.
[Efficient Approach] Using Distributive Property of GCD and LCM
An efficient approach can be done by the fact that the following version of distributivity holds true:
GCD(LCM (x, y), LCM (x, z)) = LCM(x, GCD(y, z))
For example, GCD(LCM(3, 4), LCM(3, 10)) = LCM(3, GCD(4, 10)) = LCM(3, 2) = 6
This reduces our work to compute the given problem statement.
// C++ program to compute value of GCD(LCM(x,y), LCM(x,z))
#include<bits/stdc++.h>
using namespace std;
// Returns value of GCD(LCM(x,y), LCM(x,z))
int findValue(int x, int y, int z)
{
int g = __gcd(y, z);
// Return LCM(x, GCD(y, z))
return (x*g)/__gcd(x, g);
}
int main()
{
int x = 30, y = 40, z = 400;
cout << findValue(x, y, z);
return 0;
}
// Java program to compute value
// of GCD(LCM(x,y), LCM(x,z))
class GFG {
// Recursive function to
// return gcd of a and b
static int __gcd(int a, int b)
{
// base case Everything divides 0
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Returns value of GCD(LCM(x,y), LCM(x,z))
static int findValue(int x, int y, int z)
{
int g = __gcd(y, z);
// Return LCM(x, GCD(y, z))
return (x * g) / __gcd(x, g);
}
// Driver code
public static void main(String[] args)
{
int x = 30, y = 40, z = 400;
System.out.print(findValue(x, y, z));
}
}
// This code is contributed by Anant Agarwal.
# Python program to compute
# value of GCD(LCM(x,y), LCM(x,z))
# Recursive function to
# return gcd of a and b
def __gcd(a, b):
# Everything divides 0
if (b == 0):
return a
return __gcd(b, a % b)
# Returns value of
# GCD(LCM(x,y), LCM(x,z))
def findValue(x, y, z):
g = __gcd(y, z)
# Return LCM(x, GCD(y, z))
return (x*g)/__gcd(x, g)
# driver code
x = 30
y = 40
z = 400
print("%d" % findValue(x, y, z))
# This code is contributed
# by Anant Agarwal.
// C# program to compute value
// of GCD(LCM(x,y), LCM(x,z))
using System;
class GFG {
// Recursive function to
// return gcd of a and b
static int __gcd(int a, int b)
{
// base case Everything divides 0
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Returns value of GCD(LCM(x,y),
// LCM(x,z))
static int findValue(int x, int y, int z)
{
int g = __gcd(y, z);
// Return LCM(x, GCD(y, z))
return (x*g) / __gcd(x, g);
}
// Driver code
public static void Main ()
{
int x = 30, y = 40, z = 400;
Console.Write(findValue(x, y, z));
}
}
// This code is contributed by
// Smitha Dinesh Semwal.
<script>
// JavaScript program to compute value of GCD(LCM(x,y), LCM(x,z))
function __gcd( a, b)
{
// Everything divides 0
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Returns value of GCD(LCM(x,y), LCM(x,z))
function findValue(x, y, z)
{
let g = __gcd(y, z);
// Return LCM(x, GCD(y, z))
return (x*g)/__gcd(x, g);
}
let x = 30, y = 40, z = 400;
document.write( findValue(x, y, z));
// This code contributed by gauravrajput1
</script>
<?php
// PHP program to compute value
// of GCD(LCM(x,y), LCM(x,z))
// Recursive function to
// return gcd of a and b
function __gcd( $a, $b)
{
// Everything divides 0
if ($b == 0)
return $a;
return __gcd($b, $a % $b);
}
// Returns value of GCD(LCM(x,y),
// LCM(x,z))
function findValue($x, $y, $z)
{
$g = __gcd($y, $z);
// Return LCM(x, GCD(y, z))
return ($x * $g)/__gcd($x, $g);
}
// Driver Code
$x = 30;
$y = 40;
$z = 400;
echo findValue($x, $y, $z);
// This code is contributed by anuj_67.
?>
Output
120
Time Complexity: O(log(min(a,b))
Auxiliary Space: O(log(min(a,b))
As a side note, vice versa is also true, i.e., gcd(x, lcm(y, z)) = lcm(gcd(x, y), gcd(x, z)
Please refer Properties of GCD for details.