Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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 StackOverflowerr计算大整数的阶乘?_Java_Algorithm_Stack Overflow_Biginteger_Factorial - Fatal编程技术网

Java StackOverflowerr计算大整数的阶乘?

Java StackOverflowerr计算大整数的阶乘?,java,algorithm,stack-overflow,biginteger,factorial,Java,Algorithm,Stack Overflow,Biginteger,Factorial,我正试图编写一个Java程序来计算一个大数的阶乘。似乎biginger无法容纳这么大的数字 下面是我编写的(简单的)代码 public static BigInteger getFactorial(BigInteger num) { if (num.intValue() == 0) return BigInteger.valueOf(1); if (num.intValue() == 1) return BigInteger.valueOf(1); re

我正试图编写一个Java程序来计算一个大数的阶乘。似乎
biginger
无法容纳这么大的数字

下面是我编写的(简单的)代码

 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))));
  }

上述程序在5022中处理的最大数,在此之后程序抛出
堆栈溢出错误。有没有其他方法来处理这个问题?

这里的问题看起来是太多了(5000个递归调用看起来像是吹出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;
}

希望这有帮助

问题不是BigInteger,而是使用递归方法调用(
getFactorial()

试试这个迭代算法:

public static BigInteger getFactorial(int num) {
    BigInteger fact = BigInteger.valueOf(1);
    for (int i = 1; i <= num; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
    return fact;
}
public静态biginger getFactorial(int num){
BigInteger事实=BigInteger.valueOf(1);

例如(int i=1;i谷歌的库有一个高度优化的阶乘实现,可以输出大整数……(它可以进行更平衡的乘法,并优化简单的移位。)

阶乘的简单实现在实际情况下不起作用


如果您有严重的需求,最好是编写一个函数(或
ln(gamma)
function)这不仅适用于整数,也适用于十进制数。请记住结果,这样您就不必使用
WeakHashMap
重复计算,您就可以开始工作了。

对于BigInteger数据类型,这不可能是最大的。引发stackoverflow异常的地方在哪里?发布更相关的代码。是的,请使用it运算算法。BigInteger做得很好,getFactorial只占用了所有的堆栈空间。@harold(+1)-我认为递归是一种有害于教授大学生的技术的另一个例子,至少在没有尾部递归的语言中是这样。这是一种智力练习,但最终对任何有趣的东西都没有用处。递归“对任何有趣的事情都没用"?Meh@john:堆栈溢出与数字溢出不同,这里的
num
类型无关。修改代码以使用静态常量
biginger.ONE
ZERO
。在算法类中,我记得有一种技术,在实现EXTER时使用Push/Pop到堆栈以避免堆栈溢出异常Java中的mely deep递归,特别是对于
A(4,1)
在Core2Duo T7250上执行大约需要28分钟。@ee.-您所描述的是用显式堆栈替换运行时堆栈。这是可以实现的,尽管以这种方式编写的大多数算法的复杂性通常高于递归版本。@templatetypedef I Concurve。Java中的标准运行时堆栈将使用标准递归方法在
A(4,1)
运行
Ackermann函数时,立即捕获StackOverflow异常。显式堆栈似乎解决了这个问题,尽管显式堆栈方法很复杂。