使用BigInteger参数调用方法时出现问题

时间:2013-02-18 13:11:45

标签: java biginteger

我遇到了麻烦,我对此无能为力。我想知道怎么称呼这种方法?

public static BigInteger factorial(BigInteger n) {
    BigInteger result = BigInteger.ONE;

    while (!n.equals(BigInteger.ZERO)) {
        result = result.multiply(n);
        n = n.subtract(BigInteger.ONE);
    }

    return result;
}

对于var = 1,我得到了它,我写了以下内容:

BigInteger kk = BigInteger.ONE;
System.out.println(factorial(kk));

但是,我很困惑如何计算61!,例如。

4 个答案:

答案 0 :(得分:3)

尝试:

 BigInteger kk = new BigInteger("61");
 System.out.println(factorial(kk));

答案 1 :(得分:3)

Java也有一个static factory method

BigInteger kk = BigInteger.valueOf(61L);

答案 2 :(得分:2)

BigInteger kk = new BigInteger("61");
System.out.println(factorial(kk));

API是您的朋友:http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html

答案 3 :(得分:1)

更改

BigInteger kk = BigInteger.ONE
System.out.println(factorial(kk));

BigInteger kk=new BigInteger("61");
System.out.println(factorial(kk));