我需要计算一个最大为130的不同大小的阶乘程序的运行时间。但是,该程序当前使用的是长数据类型,并且不足以容纳输出。在20阶乘之后,它变为负数并且出现故障。如何存储更大的数字,以便计算最多130个阶乘?以下是完成工作的方法:
public class FactorialCalculation {
long startTime = System.nanoTime();
//recursive Factorial method
public long factorial(long number) {
if (number <= 1)
return 1;
else
return number * factorial(number - 1);
}
//Now output the factorials of 0 through 15
public void displayFactorials() {
// Calculate the factorial of o through 15
for (int counter = 0; counter <= 130; counter++)
System.out.print("Factorial of:" + counter + " " + factorial(counter) + " \n");
long estimatedTime = System.nanoTime() - startTime;
System.out.println(estimatedTime);
} // end of the method displayFactorials
} // end of class FactorialCalculation
这是输出显示它变为负数然后变为0。
Factorial of:0 1
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
Factorial of:31 4999213071378415616
Factorial of:32 -6045878379276664832
Factorial of:33 3400198294675128320
Factorial of:34 4926277576697053184
Factorial of:35 6399018521010896896
Factorial of:36 9003737871877668864
Factorial of:37 1096907932701818880
Factorial of:38 4789013295250014208
Factorial of:39 2304077777655037952
Factorial of:40 -70609262346240000
Factorial of:41 -2894979756195840000
Factorial of:42 7538058755741581312
Factorial of:43 -7904866829883932672
Factorial of:44 2673996885588443136
Factorial of:45 -8797348664486920192
Factorial of:46 1150331055211806720
Factorial of:47 -1274672626173739008
Factorial of:48 -5844053835210817536
Factorial of:49 8789267254022766592
Factorial of:50 -3258495067890909184
Factorial of:51 -162551799050403840
Factorial of:52 -8452693550620999680
Factorial of:53 -5270900413883744256
Factorial of:54 -7927461244078915584
Factorial of:55 6711489344688881664
Factorial of:56 6908521828386340864
Factorial of:57 6404118670120845312
Factorial of:58 2504001392817995776
Factorial of:59 162129586585337856
Factorial of:60 -8718968878589280256
Factorial of:61 3098476543630901248
Factorial of:62 7638104968020361216
Factorial of:63 1585267068834414592
Factorial of:64 -9223372036854775808
Factorial of:65 -9223372036854775808
Factorial of:66 0
Factorial of:67 0
Factorial of:68 0
Factorial of:69 0
Factorial of:70 0
Factorial of:71 0
Factorial of:72 0
Factorial of:73 0
Factorial of:74 0
Factorial of:75 0
Factorial of:76 0
Factorial of:77 0
Factorial of:78 0
Factorial of:79 0
Factorial of:80 0
Factorial of:81 0
Factorial of:82 0
Factorial of:83 0
Factorial of:84 0
Factorial of:85 0
Factorial of:86 0
Factorial of:87 0
Factorial of:88 0
Factorial of:89 0
Factorial of:90 0
Factorial of:91 0
Factorial of:92 0
Factorial of:93 0
Factorial of:94 0
Factorial of:95 0
Factorial of:96 0
Factorial of:97 0
Factorial of:98 0
Factorial of:99 0
Factorial of:100 0
Factorial of:101 0
Factorial of:102 0
Factorial of:103 0
Factorial of:104 0
Factorial of:105 0
Factorial of:106 0
Factorial of:107 0
Factorial of:108 0
Factorial of:109 0
Factorial of:110 0
Factorial of:111 0
Factorial of:112 0
Factorial of:113 0
Factorial of:114 0
Factorial of:115 0
Factorial of:116 0
Factorial of:117 0
Factorial of:118 0
Factorial of:119 0
Factorial of:120 0
Factorial of:121 0
Factorial of:122 0
Factorial of:123 0
Factorial of:124 0
Factorial of:125 0
Factorial of:126 0
Factorial of:127 0
Factorial of:128 0
Factorial of:129 0
Factorial of:130 0
答案 0 :(得分:4)
BigInteger适用于此应用程序。
如果您阅读了javadoc,则保证 *
BigInteger
能够支持最多2个 MAXINT 的数字;即2 2147483647 。那是(非常粗略地)7.9 * 10 646456992 。
130!约为6.466855489220473e + 219或6.46 * 10 219 。正如您所看到的,与保证可表示的最大值相比,这是微不足道的。
*
- 当然,此保证假定您有足够的堆空间。但我们正在讨论(MAXINT / 8)字节... 0.25 GB ......这在现代64位计算机上并不是很大。
答案 1 :(得分:0)
看看BigInteger。这可以存储非常大的值,大小未明确知道,但由于它是以int[]
实现的,因此可能正如this question.
答案 2 :(得分:0)
请查看以下代码段,这将符合要求
您可以使用BigInteger/BigDecimal
而不是long
数据类型来实现此目的。
long startTime = System.nanoTime();
public static void main(String[] args) {
Test t = new Test();
t.displayFactorials();
}
//recursive Factorial method
public BigDecimal factorial(BigDecimal number)
{
if (number.intValue() <= 1)
return BigDecimal.ONE;
else
return number.multiply( factorial ( number.subtract(BigDecimal.ONE) ) );
}
//Now output the factorials of 0 through 15
public void displayFactorials()
{
// Calculate the factorial of o through 15
for ( int counter = 0; counter <= 130; counter++ )
System.out.print ( "Factorial of:" + counter + " " +
factorial ( new BigDecimal(counter+"") ) + " \n");
long estimatedTime = System.nanoTime() - startTime;
System.out.println(estimatedTime);
} // end of the method displayFactorials
您也可以将BigDecimal
更改为BigInteger
。