📌  相关文章
📜  超过两个(或数组)数字的GCD

📅  最后修改于: 2021-04-27 16:55:49             🧑  作者: Mango

给定一个数字数组,找到该数组元素的GCD。在上一篇文章中,我们发现GCD为两个数字。
例子:

Input  : arr[] = {1, 2, 3}
Output : 1

Input  : arr[] = {2, 4, 6, 8}
Output : 2

三个或更多数字的GCD等于所有数字共有的质数因子的乘积,但也可以通过重复获取数字对的GCD来计算。

gcd(a, b, c) = gcd(a, gcd(b, c)) 
             = gcd(gcd(a, b), c) 
             = gcd(gcd(a, c), b)

对于元素数组,我们执行以下操作。如果任何一步的结果都变为1,我们还将检查结果,我们将以gcd(1,x)= 1的形式返回1。

result = arr[0]
For i = 1 to n-1
   result = GCD(result, arr[i])

以下是上述想法的实现。

C++
// C++ program to find GCD of two or
// more numbers
#include 
using namespace std;
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to find gcd of array of
// numbers
int findGCD(int arr[], int n)
{
    int result = arr[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);
 
        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findGCD(arr, n) << endl;
    return 0;
}


JAVA
// Java program to find GCD of two or
// more numbers
 
public class GCD {
    // Function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
 
    // Function to find gcd of array of
    // numbers
    static int findGCD(int arr[], int n)
    {
        int result = 0;
        for (int element: arr){
            result = gcd(result, element);
 
            if(result == 1)
            {
               return 1;
            }
        }
 
        return result;
    }
 
    public static void main(String[] args)
    {
        int arr[] = { 2, 4, 6, 8, 16 };
        int n = arr.length;
        System.out.println(findGCD(arr, n));
    }
}
 
// This code is contributed by Saket Kumar


Python
# GCD of more than two (or array) numbers
 
# Function implements the Euclidian
# algorithm to find H.C.F. of two number
def find_gcd(x, y):
     
    while(y):
        x, y = y, x % y
     
    return x
         
# Driver Code       
l = [2, 4, 6, 8, 16]
 
num1 = l[0]
num2 = l[1]
gcd = find_gcd(num1, num2)
 
for i in range(2, len(l)):
    gcd = find_gcd(gcd, l[i])
     
print(gcd)
 
# Code contributed by Mohit Gupta_OMG


C#
// C# program to find GCD of
// two or more numbers
using System;
 
public class GCD {
     
    // Function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
 
    // Function to find gcd of
    // array of numbers
    static int findGCD(int[] arr, int n)
    {
        int result = arr[0];
        for (int i = 1; i < n; i++){
            result = gcd(arr[i], result);
 
            if(result == 1)
            {
               return 1;
            }
        }
 
        return result;
    }
     
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 4, 6, 8, 16 };
        int n = arr.Length;
        Console.Write(findGCD(arr, n));
    }
}
 
// This code is contributed by nitin mittal


PHP


Javascript


输出:

2