这是我的阶乘方法:
到目前为止,一切看起来都很好,输出结果似乎是正确的,但有些阶乘是负数而不是正数:
public static long factorial(int num1) {
if (num1 <= 1)
return 1;
else
return num1 * factorial(num1 - 1);
}
这就是递归阶乘方法:
for (i = 0; i <= 25; i++)
System.out.printf ("%d != %,d\n", i, factorial (i));
到目前为止,一切看起来都很好,输出结果似乎是正确的,但有些阶乘是负数而不是正数:
OUTPUT:
0 != 1
1 != 1
2 != 2
3 != 6
4 != 24
5 != 120
6 != 720
7 != 5,040
8 != 40,320
9 != 362,880
10 != 3,628,800
11 != 39,916,800
12 != 479,001,600
13 != 6,227,020,800
14 != 87,178,291,200
15 != 1,307,674,368,000
16 != 20,922,789,888,000
17 != 355,687,428,096,000
18 != 6,402,373,705,728,000
19 != 121,645,100,408,832,000
20 != 2,432,902,008,176,640,000
21 != -4,249,290,049,419,214,848
22 != -1,250,660,718,674,968,576
23 != 8,128,291,617,894,825,984
24 != -7,835,185,981,329,244,160
25 != 7,034,535,277,573,963,776
21、22和24是负数,为什么会出现这种情况?这是一种“除以0”的悖论吗?
long
中适合的最大阶乘是20! - Daniel Fischer