📜  查找两个数字的GCD或HCF的程序

📅  最后修改于: 2021-05-04 23:29:21             🧑  作者: Mango

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

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

一个简单的解决方案是找到两个数字的所有素数,然后找到两个数字中存在的所有因子的交集。最后返回交集中元素的乘积。
一个有效的解决方案是使用欧几里得算法,这是用于此目的的主要算法。这个想法是,如果从较大的数字中减去较小的数字,则两个数字的GCD不会改变。

C++
// C++ program to find GCD of two numbers
#include 
using namespace std;
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
    // Everything divides 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 = 98, b = 56;
    cout<<"GCD of "<


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)
       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 = 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)
    {
        // Everything divides 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):
        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)
          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 
using namespace std;
// 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;
    cout<<"GCD of "<


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 
using namespace std;
// 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;
    cout<<"GCD of "<

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

上面算法的时间复杂度为O(log(max(a,b))),这是通过对最坏情况的分析得出的。我们要做的是,问走1步的2个最小数字是(1,1)。如果我们想将步数增加到2,同时将步数保持在尽可能低的水平,则可以将步数设为(1,2)。类似地,对于3个步骤,数字将为(2,3),4将为(3,5),5将为(5,8)。因此,我们可以在此处注意到一种模式,对于第n步,数字将为(fib(n),fib(n + 1))。因此,最坏情况下的时间复杂度将是O(n),其中a> = fib(n)和b> = fib(n + 1)。

现在,斐波那契数列是一个指数增长的序列,其中第n /(n-1)个项的比率接近(sqrt(5)-1)/ 2,也称为黄金比率。因此我们可以看到,算法的时间复杂度随着项的增长而线性增加,因此时间复杂度将为log(max(a,b))。