for (int i = 0; i <= 25; i++)
System.out.printf("%d! = %,d\n", i, factorial(i));
上述代碼初始化階乘方法如下:因子函數產生21錯誤的結果!和上述
public static long factorial(int num1)
{
if (num1 == 0)
return 1;
else
return Math.abs(num1 * factorial(num1 - 1));
}
作爲所以下面的輸出被創建:
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結果!是錯誤的(應該是51090942171709440000),並且結果變得完全不合適22!以上。誰能解釋爲什麼?
'10!'後它怎麼不穩定?對我來說,直到'21!'看起來很好,對於'長'來說它最終變得太大了。 –