This question already has an answer here:
这个问题在这里已有答案:
- *Error computing factorial of a BigInteger? 5 answers
*Error计算BigInteger的阶乘? 5个答案
I've been working around with this recursive function
but couldn't find myself which led me to overflow error
and it keeps coming around. I've already tried casting to BigInteger
also but nothing special is coming. I don't see any warning or syntax error in my Eclipse IDE
. I had to submit an efficient algorithm for big numbers. thanks in advance. :-)
我一直在处理这个递归函数,但是找不到自己导致我出现溢出错误并且它一直存在。我已经尝试过投射到BigInteger,但没有什么特别的。我的Eclipse IDE中没有看到任何警告或语法错误。我必须为大数字提交一个有效的算法。提前致谢。 :-)
public static BigInteger factorial(int n)
{
return n > 2 ? new BigInteger(n+"").multiply(factorial(n-1)) : new BigInteger(n+"");
}
1 个解决方案
#1
1
The problem
You're getting the error because the computer has to remember every method call you make (and other information) until that method call is finished, and there's only so much space on the "stack" set aside to remember all that.
你得到错误是因为计算机必须记住你所做的每个方法调用(以及其他信息),直到该方法调用结束,并且“堆栈”上只有这么多空间留出来记住所有这些。
You recurse so many times that you overflow the stack space set up to remember method calls that are in progress. That's called a stack overflow.
你进行了多次递归,以便溢出设置的堆栈空间以记住正在进行的方法调用。这称为堆栈溢出。
A possible solution
A reasonably-efficient algorithm is to use a simple loop. This has the side benefit of not causing stack overflows, since you don't create more method calls over and over again, you just do stuff inside the first method call.
一种合理有效的算法是使用简单的循环。这样做的好处是不会导致堆栈溢出,因为你不会一遍又一遍地创建更多的方法调用,你只需要在第一个方法调用中执行操作。
You should also use BigInteger.valueOf(n)
instead of new BigInteger(n+"")
:
您还应该使用BigInteger.valueOf(n)而不是新的BigInteger(n +“”):
public static BigInteger factorial(int n) {
BigInteger result = BigInteger.ONE;
for (; n > 1; n--) {
result = result.multiply(BigInteger.valueOf(n));
}
return result;
}
This takes my computer about 6 seconds to compute 100,000!
这需要我的电脑大约6秒来计算100,000!
More efficient solutions
There are faster algorithms than this. See another question and its links for more details.
有比这更快的算法。有关详细信息,请参阅其他问题及其链接。
#1
1
The problem
You're getting the error because the computer has to remember every method call you make (and other information) until that method call is finished, and there's only so much space on the "stack" set aside to remember all that.
你得到错误是因为计算机必须记住你所做的每个方法调用(以及其他信息),直到该方法调用结束,并且“堆栈”上只有这么多空间留出来记住所有这些。
You recurse so many times that you overflow the stack space set up to remember method calls that are in progress. That's called a stack overflow.
你进行了多次递归,以便溢出设置的堆栈空间以记住正在进行的方法调用。这称为堆栈溢出。
A possible solution
A reasonably-efficient algorithm is to use a simple loop. This has the side benefit of not causing stack overflows, since you don't create more method calls over and over again, you just do stuff inside the first method call.
一种合理有效的算法是使用简单的循环。这样做的好处是不会导致堆栈溢出,因为你不会一遍又一遍地创建更多的方法调用,你只需要在第一个方法调用中执行操作。
You should also use BigInteger.valueOf(n)
instead of new BigInteger(n+"")
:
您还应该使用BigInteger.valueOf(n)而不是新的BigInteger(n +“”):
public static BigInteger factorial(int n) {
BigInteger result = BigInteger.ONE;
for (; n > 1; n--) {
result = result.multiply(BigInteger.valueOf(n));
}
return result;
}
This takes my computer about 6 seconds to compute 100,000!
这需要我的电脑大约6秒来计算100,000!
More efficient solutions
There are faster algorithms than this. See another question and its links for more details.
有比这更快的算法。有关详细信息,请参阅其他问题及其链接。