Java的gcd函数 求最大公约数

今天第一次用到,暂存一下。

传入a和b,返回公约数

public static int gcd(int a,int b) {
        return b==0?a:gcd(b, a%b);
}
public static BigInteger gcd(BigInteger a, BigInteger b) {
        return b.compareTo(BigInteger.ZERO) == 0 ? a : gcd(b, a.mod(b));
}

你可能感兴趣的:(Java,算法,java,算法)