Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用BigInteger的GCD数的乘积_Java_Function_Biginteger_Greatest Common Divisor - Fatal编程技术网

Java 使用BigInteger的GCD数的乘积

Java 使用BigInteger的GCD数的乘积,java,function,biginteger,greatest-common-divisor,Java,Function,Biginteger,Greatest Common Divisor,//我想知道这个代码有什么问题 public class Solution { public static BigInteger findGCD(BigInteger number1, BigInteger number2) { if(number2.intValue() == 0){ return number1; } return findGCD(number2, number1.mod(number2));

//我想知道这个代码有什么问题

public class Solution {

    public static BigInteger findGCD(BigInteger number1, BigInteger number2) {
        if(number2.intValue() == 0){
            return number1;
        }
        return findGCD(number2, number1.mod(number2));
    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BigInteger i,j;
        BigInteger pro = new BigInteger("1");
        Scanner in = new Scanner(System.in);
        BigInteger N = in.nextBigInteger();
        BigInteger M = in.nextBigInteger();
        BigInteger one = new BigInteger("1");
        for(i=one;i.equals(N);i.add(one)){
            for(j=one;j.equals(M);j.add(one)){
                BigInteger a = findGCD(i,j);

                pro = pro.multiply(a);
                System.out.println(pro);
            }
        }
        System.out.println(pro);
    }
}

//我想找出GCD输出的产品。

我可以看到三个错误

首先,
if(number2.intValue()==0)
应该是
if(number2.equals(biginger.ZERO))
。这是因为
intValue()
只查看32位,而不是整数

其次,
i.add(one)
应该是
i=i.add(one)
i.add(一)
计算出比
i
大的
biginger
1,但它不会更改
i
的值

第三,我假设你的意思是(I=1;!I.equals(N);…)的

另外,
biginger
类中已经有了一个
gcd
方法!

看看gcd算法吧