📜  计算 GCD 的Java程序

📅  最后修改于: 2022-05-13 01:54:39.607000             🧑  作者: Mango

计算 GCD 的Java程序

两个给定数AB的 GCD(最大公约数)是可以完全整除 A 和 B 的最大数,即在每种情况下都留下余数 0。 GCD 也称为 HCF(最高公因数)。有多种方法可以找到两个给定数字的 GCD。

方法:

可以使用不同的方法计算给定两个数字 A 和 B 的 GCD。

  1. 一般方法
  2. 欧几里德算法(通过重复减法)
  3. 欧几里德算法(通过重复除法)

例子:

Input: 20, 30
Output: GCD(20, 30) = 10
Explanation: 10 is the highest integer which divides both 20 and 30 leaving 0 remainder

Input: 36, 37
Output: GCD(36, 37) = 1
Explanation: 36 and 37 don't have any factors in common except 1. So, 1 is the gcd of 36 and 37

注意:如果 A、B 是互质数,则 gcd(A, B) = 1。

一般的做法:

在计算 GCD 的一般方法中,我们实际上实现了 GCD 的定义。

  • 首先,分别找出A和B的所有因子。
  • 然后列出 A 和 B 共有的那些因素。
  • 这些公因子中最高的是 A 和 B 的 GCD。

例子:

A = 20, B = 30
Factors of A : (1, 2, 4, 5, 10, 20)
Factors of B : (1, 2, 3, 5, 6, 10, 15, 30)
Common factors of A and B : (1, 2, 5, 10)
Highest of the Common factors (GCD) = 10

很明显,20 和 30 的 GCD 不能大于 20。所以我们必须检查 1 和 20 范围内的数字。另外,我们需要最大的除数。因此,从后向迭代以减少计算时间。

Java
// Java program to compute GCD of
// two numbers using general
// approach
import java.io.*;
 
class GFG {
 
    // gcd() method, returns the GCD of a and b
    static int gcd(int a, int b)
    {
        // stores minimum(a, b)
        int i;
        if (a < b)
            i = a;
        else
            i = b;
 
        // take a loop iterating through smaller number to 1
        for (i = i; i > 1; i--) {
 
            // check if the current value of i divides both
            // numbers with remainder 0 if yes, then i is
            // the GCD of a and b
            if (a % i == 0 && b % i == 0)
                return i;
        }
 
        // if there are no common factors for a and b other
        // than 1, then GCD of a and b is 1
        return 1;
    }
    // Driver method
    public static void main(String[] args)
    {
        int a = 30, b = 20;
 
        // calling gcd() method over
        // the integers 30 and 20
        System.out.println("GCD = " + gcd(b, a));
    }
}


Java
// Java program to compute GCD
// of two numbers using Euclid's
// repeated subtraction approach
import java.io.*;
 
class GFG {
 
    // gcd method returns the GCD of a and b
    static int gcd(int a, int b)
    {
        // if b=0, a is the GCD
        if (b == 0)
            return a;
 
        // call the gcd() method recursively by
        // replacing a with b and b with
        // difference(a,b) as long as b != 0
        else
            return gcd(b, Math.abs(a - b));
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int a = 30, b = 20;
 
        // calling gcd() over
        // integers 30 and 20
        System.out.println("GCD = " + gcd(a, b));
    }
}


Java
// Java program to compute GCD
// of two numbers using Euclid's
// repeated division approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // gcd method returns the GCD of a and b
    static int gcd(int a, int b)
    {
        // if b=0, a is the GCD
        if (b == 0)
            return a;
 
        // call the gcd() method recursively by
        // replacing a with b and b with
        // modulus(a,b) as long as b != 0
        else
            return gcd(b, a % b);
    }
    // Driver method
    public static void main(String[] args)
    {
        int a = 20, b = 30;
 
        // calling gcd() over
        // integers 30 and 20
        System.out.println("GCD = " + gcd(a, b));
    }
}


输出
GCD = 10

欧几里德算法(重复减法):

这种方法是基于这样的原则,即使我们用A和B之间的差异替换较大的数,A和B两个数的GCD也会相同。 在这种方法中,我们通过替换对A和B重复进行GCD操作A 与 B 和 B 的差值 (A, B) 只要差值大于 0。

例子

A = 30, B = 20
gcd(30, 20) -> gcd(A, B)
gcd(20, 30 - 20) = gcd(20,10) -> gcd(B,B-A)
gcd(30 - 20, 20 - (30 - 20)) = gcd(10, 10) -> gcd(B - A, B - (B - A))
gcd(10, 10 - 10) = gcd(10, 0)
here, the difference is 0
So stop the procedure. And 10 is the GCD of 30 and 20

Java

// Java program to compute GCD
// of two numbers using Euclid's
// repeated subtraction approach
import java.io.*;
 
class GFG {
 
    // gcd method returns the GCD of a and b
    static int gcd(int a, int b)
    {
        // if b=0, a is the GCD
        if (b == 0)
            return a;
 
        // call the gcd() method recursively by
        // replacing a with b and b with
        // difference(a,b) as long as b != 0
        else
            return gcd(b, Math.abs(a - b));
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int a = 30, b = 20;
 
        // calling gcd() over
        // integers 30 and 20
        System.out.println("GCD = " + gcd(a, b));
    }
}
输出
GCD = 10

欧几里得算法(重复除法):

这种方法类似于重复减法方法。但是,在这种方法中,我们用 A 和 B 的模数代替差值来代替 B。

例子 :

A = 30, B = 20
gcd(30, 20) -> gcd(A, B)
gcd(20, 30 % 20) = gcd(20, 10) -> gcd(B, A % B)
gcd(10, 20 % 10) = gcd(10, 10) -> gcd(A % B, B % (A % B))
gcd(10, 10 % 10) = gcd(10, 0)
here, the modulus became 0
So, stop the procedure. And 10 is the GCD of 30 and 20

Java

// Java program to compute GCD
// of two numbers using Euclid's
// repeated division approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // gcd method returns the GCD of a and b
    static int gcd(int a, int b)
    {
        // if b=0, a is the GCD
        if (b == 0)
            return a;
 
        // call the gcd() method recursively by
        // replacing a with b and b with
        // modulus(a,b) as long as b != 0
        else
            return gcd(b, a % b);
    }
    // Driver method
    public static void main(String[] args)
    {
        int a = 20, b = 30;
 
        // calling gcd() over
        // integers 30 and 20
        System.out.println("GCD = " + gcd(a, b));
    }
}
输出
GCD = 10

Euclid 的重复除法方法是所有方法中最常用的。