相关疑难解决方法(0)

StackOverflowError计算BigInteger的阶乘?

我正在尝试编写一个Java程序来计算大数的阶乘.似乎BigInteger无法容纳这么大的数字.

以下是我写的(直截了当的)代码.

 public static BigInteger getFactorial(BigInteger num) {
      if (num.intValue() == 0) return BigInteger.valueOf(1);

      if (num.intValue() == 1) return BigInteger.valueOf(1);

      return num.multiply(getFactorial(num.subtract(BigInteger.valueOf(1))));
  }
Run Code Online (Sandbox Code Playgroud)

上述程序在5022中处理的最大数量,之后程序抛出一个StackOverflowError.有没有其他方法来处理它?

java stack-overflow algorithm biginteger factorial

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

algorithm ×1

biginteger ×1

factorial ×1

java ×1

stack-overflow ×1