我编写了以下 Java 代码来递归地计算从 1 到 30 的数字的阶乘。由于某种原因,输出与 results here 不匹配对于大于 20 的数字。我也很惊讶地看到负数。
代码
class Test {
public static Long factorial(Long number) {
if(number == 1){
return 1L;
}else{
return number*factorial(number-1);
}
}
public static void main(final String... arguments){
for(Long number=1L;number<=30;number++){
System.out.println("Factorial of " + number + " : " + factorial(number));
}
}
}
输出
Factorial of 1 : 1
Factorial of 2 : 2
Factorial of 3 : 6
Factorial of 4 : 24
Factorial of 5 : 120
Factorial of 6 : 720
Factorial of 7 : 5040
Factorial of 8 : 40320
Factorial of 9 : 362880
Factorial of 10 : 3628800
Factorial of 11 : 39916800
Factorial of 12 : 479001600
Factorial of 13 : 6227020800
Factorial of 14 : 87178291200
Factorial of 15 : 1307674368000
Factorial of 16 : 20922789888000
Factorial of 17 : 355687428096000
Factorial of 18 : 6402373705728000
Factorial of 19 : 121645100408832000
Factorial of 20 : 2432902008176640000
Factorial of 21 : -4249290049419214848
Factorial of 22 : -1250660718674968576
Factorial of 23 : 8128291617894825984
Factorial of 24 : -7835185981329244160
Factorial of 25 : 7034535277573963776
Factorial of 26 : -1569523520172457984
Factorial of 27 : -5483646897237262336
Factorial of 28 : -5968160532966932480
Factorial of 29 : -7055958792655077376
Factorial of 30 : -8764578968847253504
有人可以帮我正确计算最多 30 的阶乘吗?
最佳答案
这就是所谓的“溢出”。 Long 以 64 位存储;它们的最大值是 2^63 - 1,即 9,223,372,036,854,775,807。而是尝试使用 BigInteger。使用 BigInteger 有点棘手,因为实例化和操作不同于普通的 int/long。这是为 BigInteger 重新编写的代码:
public class BigIntFactorial {
public static BigInteger factorial(BigInteger number) {
if(number.equals(BigInteger.ONE)) {
return BigInteger.valueOf(1);
} else {
return number.multiply(factorial(number.subtract(BigInteger.ONE)));
}
}
public static void main(final String... arguments){
for(Long number=1L;number<=30;number++){
System.out.println("Factorial of " + number + " : " + factorial(BigInteger.valueOf(number)));
}
}
}
输出是:
Factorial of 1 : 1
Factorial of 2 : 2
Factorial of 3 : 6
Factorial of 4 : 24
Factorial of 5 : 120
Factorial of 6 : 720
Factorial of 7 : 5040
Factorial of 8 : 40320
Factorial of 9 : 362880
Factorial of 10 : 3628800
Factorial of 11 : 39916800
Factorial of 12 : 479001600
Factorial of 13 : 6227020800
Factorial of 14 : 87178291200
Factorial of 15 : 1307674368000
Factorial of 16 : 20922789888000
Factorial of 17 : 355687428096000
Factorial of 18 : 6402373705728000
Factorial of 19 : 121645100408832000
Factorial of 20 : 2432902008176640000
Factorial of 21 : 51090942171709440000
Factorial of 22 : 1124000727777607680000
Factorial of 23 : 25852016738884976640000
Factorial of 24 : 620448401733239439360000
Factorial of 25 : 15511210043330985984000000
Factorial of 26 : 403291461126605635584000000
Factorial of 27 : 10888869450418352160768000000
Factorial of 28 : 304888344611713860501504000000
Factorial of 29 : 8841761993739701954543616000000
Factorial of 30 : 265252859812191058636308480000000
关于java - 计算阶乘的递归函数在 20 之后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45194920/