📜  查找2个数字的HCF(最高公因数)的程序

📅  最后修改于: 2021-04-30 03:34:47             🧑  作者: Mango

两个数字的HCF(最高公因数)或GCD(最大公因数)是将两者相除的最大数。

GCD

例如20和28的GCD为4,98和56的GCD为14。

一个简单的解决方案是找到两个数字的所有素数,然后找到两个数字中存在的所有因子的交集。最后返回交集中元素的乘积。

一个有效的解决方案是使用欧几里得算法,这是用于此目的的主要算法。这个想法是,如果从较大的数字中减去较小的数字,则两个数字的GCD不会改变。

C
// C program to find GCD of two numbers
#include 
 
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
    // Everything divides 0
    if (a == 0 && b == 0)
        return 0;
    if (a == 0)
        return b;
    if (b == 0)
        return a;
 
    // base case
    if (a == b)
        return a;
 
    // a is greater
    if (a > b)
        return gcd(a - b, b);
    return gcd(a, b - a);
}
 
// Driver program to test above function
int main()
{
    int a = 0, b = 56;
    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
    return 0;
}


Java
// Java program to find GCD of two numbers
class Test {
    // 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;
        if (a == 0)
            return b;
        if (b == 0)
            return a;
 
        // base case
        if (a == b)
            return a;
 
        // a is greater
        if (a > b)
            return gcd(a - b, b);
        return gcd(a, b - a);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int a = 98, b = 56;
        System.out.println("GCD of " + a + " and " + b
                           + " is " + gcd(a, b));
    }
}


Python3
# Recursive function to return gcd of a and b
def gcd(a, b):
 
    # Everything divides 0
    if(a == 0 and b == 0):
        return 0
       
    if(a == 0):
        return b
     
    if(b == 0):
        return a
     
    # base case
    if(a == b):
        return a
 
    # a is greater
    if (a > b):
        return gcd(a-b, b)
    return gcd(a, b-a)
 
# Driver program to test above function
a = 98
b = 56
if(gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')
 
# This code is contributed by Danish Raza


C#
// C# program to find GCD of two
// numbers
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;
        if (a == 0)
            return b;
        if (b == 0)
            return a;
 
        // base case
        if (a == b)
            return a;
 
        // a is greater
        if (a > b)
            return gcd(a - b, b);
 
        return gcd(a, b - a);
    }
 
    // Driver method
    public static void Main()
    {
        int a = 98, b = 56;
        Console.WriteLine("GCD of " + a + " and " + b
                          + " is " + gcd(a, b));
    }
}
 
// This code is contributed by anuj_67.


PHP
 $b)
        return gcd( $a-$b , $b ) ;
 
    return gcd( $a , $b-$a ) ;
}
 
// Driver code
$a = 98 ;
$b = 56 ;
 
echo "GCD of $a and $b is ", gcd($a , $b) ;
 
// This code is contributed by Anivesh Tiwari
?>


Javascript


C
// C program to find GCD of two numbers
#include 
 
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Driver program to test above function
int main()
{
    int a = 98, b = 56;
    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
    return 0;
}


Java
// Java program to find GCD of two numbers
class Test
{
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
      if (b == 0)
        return a;
      return gcd(b, a % b);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int a = 98, b = 56;
        System.out.println("GCD of " + a +" and " + b +
                                   " is " + gcd(a, b));
    }
}


Python3
# 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)
 
# Driver program to test above function
a = 98
b = 56
if(gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')
 
# This code is contributed by Danish Raza


C#
// C# program to find GCD of two
// numbers
using System;
 
class GFG {
     
    // Recursive function to return
    // gcd of a and b
    static int gcd(int a, int b)
    {     
       if (b == 0)
          return a;
       return gcd(b, a % b);
    }
     
    // Driver method
    public static void Main()
    {
        int a = 98, b = 56;
        Console.WriteLine("GCD of "
          + a +" and " + b + " is "
                      + gcd(a, b));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出:

GCD of 98 and 56 is 14

一种更有效的解决方案是在欧几里得算法中使用模运算符。

C

// C program to find GCD of two numbers
#include 
 
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Driver program to test above function
int main()
{
    int a = 98, b = 56;
    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
    return 0;
}

Java

// Java program to find GCD of two numbers
class Test
{
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
      if (b == 0)
        return a;
      return gcd(b, a % b);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int a = 98, b = 56;
        System.out.println("GCD of " + a +" and " + b +
                                   " is " + gcd(a, b));
    }
}

Python3

# 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)
 
# Driver program to test above function
a = 98
b = 56
if(gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')
 
# This code is contributed by Danish Raza

C#

// C# program to find GCD of two
// numbers
using System;
 
class GFG {
     
    // Recursive function to return
    // gcd of a and b
    static int gcd(int a, int b)
    {     
       if (b == 0)
          return a;
       return gcd(b, a % b);
    }
     
    // Driver method
    public static void Main()
    {
        int a = 98, b = 56;
        Console.WriteLine("GCD of "
          + a +" and " + b + " is "
                      + gcd(a, b));
    }
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出:

GCD of 98 and 56 is 14