📜  GCD,LCM和分配财产

📅  最后修改于: 2021-04-23 18:23:16             🧑  作者: Mango

给定三个整数x,y,z,任务是计算GCD(LCM(x,y),LCM(x,z))的值
其中,GCD =最大公约数,LCM =最小公倍数
例子:

Input: x = 15, y = 20, z = 100
Output: 60

Input: x = 30, y = 40, z = 400
Output: 120

解决该问题的一种方法是找到GCD(x,y),然后使用它找到LCM(x,y)。同样,我们找到LCM(x,z),然后最终找到所得结果的GCD。
以下版本的分布适用于以下事实,可以实现一种有效的方法:
GCD(LCM(x,y),LCM(x,z))= LCM(x,GCD(y,z))
例如,GCD(LCM(3,4),LCM(3,10))= LCM(3,GCD(4,10))= LCM(3,2)= 6
这减少了我们计算给定问题陈述的工作。

C++
// C++ program to compute value of GCD(LCM(x,y), LCM(x,z))
#include
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
// 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)
    {
        // Everything divides 0
        if (a == 0 || b == 0)
        return 0;
     
        // base case
        if (a == b)
            return a;
     
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
        return __gcd(a, b - a);
    }
     
    // 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.


Python3
# 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 (a == 0 or b == 0):
        return 0
  
    # base case
    if (a == b):
        return a
  
    # a is greater
    if (a > b):
        return __gcd(a-b, b)
    return __gcd(a, b-a)
 
# 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#
// 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)
    {
         
        // Everything divides 0
        if (a == 0 || b == 0)
        return 0;
     
        // base case
        if (a == b)
            return a;
     
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
        return __gcd(a, b - a);
    }
     
    // 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.


PHP
 $b)
        return __gcd($a - $b, $b);
    return __gcd($a, $b - $a);
}
 
// 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.
?>


Javascript


输出:

120