两个以上(或数组)数 GCD 的 Java 程序
原文:https://www . geesforgeks . org/Java-program-for-gcd-of-two-or-array-numbers/
三个或三个以上数的 GCD 等于所有数共有的质因数的乘积,但也可以通过重复取数对的 GCD 来计算。
gcd(a, b, c) = gcd(a, gcd(b, c))
= gcd(gcd(a, b), c)
= gcd(gcd(a, c), b)
// 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 = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
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
输出:
2
更多详情请参考完整的两个以上(或数组)数字的 GCD 文章!
版权属于:月萌API www.moonapi.com,转载请注明出处