BigInteger用法-Java大数据存储、运算

用这样一组数字,0,1,1,2,3,5,8........,求第100个数是多少!

初看这道题很简单,我们只需要设定一个数组,然后往里面放入对应的值,除了前两个数字位,后面每个数字位都是前两个数字位的和。刚开始我用了int数组来存放这组数,发现根本放不下去,到了第47位数的时候就存在数值溢出了,然后我换为long型数组,同样放不下去,到了第93位数的时候也存在数值溢出了。这样,只能用BigInteger类了,这是java提供的可以存储任意精度的整数的类,用它我们不需要考虑数值溢出的错误。即使我们求第1000个数值也没有什么问题。不过,它的数值运算必须以方法调用方式取代运算符方式来实现,由于这么做复杂了许多,运算速度会比较慢,所以在确定数值不会溢出的情况下,是不推荐用这种型式来进行数值存储和运算的。以下是具体代码。

补充知识:

System.out.println("Integer的最大值:"+Integer.MAX_VALUE);
System.out.println("Long的最大值:"+Long.MAX_VALUE);
Integer的最大值:2147483647   //10位(十进制)
Long的最大值:9223372036854775807   //19位(十进制)
BigInteger类型无限制大,故没最大值
 
  

 
  

package test;

import java.math.BigInteger;

public class BigIntegertest {
	public static void main(String[] args) {
		int num = 100;
		//整型数组
		int[] resultInt = new int[100];
		resultInt[0] = 0;
		resultInt[1] = 1;
		//长整型数组
		long[] resultLong = new long[100];
		resultLong[0] = 0L;
		resultLong[1] = 1L;
		//BigInteger数组
		BigInteger[] resultBigInteger = new BigInteger[100];
		resultBigInteger[0] = BigInteger.ZERO;
		resultBigInteger[1] = BigInteger.ONE;
		for (int i = 2; i < num; i++) {
			resultInt[i] = resultInt[i - 1] + resultInt[i - 2];
			resultLong[i] = resultLong[i - 1] + resultLong[i - 2];
			resultBigInteger[i] = resultBigInteger[i - 1].add(resultBigInteger[i - 2]);
		}
		System.out.println("--------------------用整型来存储数值的结果!---------------------");
		for (int i = 0; i < resultInt.length; i++) {
			System.out.println("resultInt[" + i + "] = "+ resultInt[i]);
		}
		System.out.println("--------------------用长整型来存储数值的结果!---------------------");
		for (int i = 0; i < resultLong.length; i++) {
			System.out.println("resultLong[" + i + "] = "+ resultLong[i]);
		}
		System.out.println("--------------------用BigInteger来存储数值的结果!---------------------");
		for (int i = 0; i < resultBigInteger.length; i++) {
			System.out.println("resultBigInteger[" + i + "] = "+ resultBigInteger[i]);
		}
	}
}
结果:

--------------------用整型来存储数值的结果!---------------------
resultInt[0] = 0
resultInt[1] = 1
resultInt[2] = 1
resultInt[3] = 2
resultInt[4] = 3
resultInt[5] = 5
resultInt[6] = 8
resultInt[7] = 13
resultInt[8] = 21
resultInt[9] = 34
resultInt[10] = 55
resultInt[11] = 89
resultInt[12] = 144
resultInt[13] = 233
resultInt[14] = 377
resultInt[15] = 610
resultInt[16] = 987
resultInt[17] = 1597
resultInt[18] = 2584
resultInt[19] = 4181
resultInt[20] = 6765
resultInt[21] = 10946
resultInt[22] = 17711
resultInt[23] = 28657
resultInt[24] = 46368
resultInt[25] = 75025
resultInt[26] = 121393
resultInt[27] = 196418
resultInt[28] = 317811
resultInt[29] = 514229
resultInt[30] = 832040
resultInt[31] = 1346269
resultInt[32] = 2178309
resultInt[33] = 3524578
resultInt[34] = 5702887
resultInt[35] = 9227465
resultInt[36] = 14930352
resultInt[37] = 24157817
resultInt[38] = 39088169
resultInt[39] = 63245986
resultInt[40] = 102334155
resultInt[41] = 165580141
resultInt[42] = 267914296
resultInt[43] = 433494437
resultInt[44] = 701408733
resultInt[45] = 1134903170
resultInt[46] = 1836311903
resultInt[47] = -1323752223
resultInt[48] = 512559680
resultInt[49] = -811192543
resultInt[50] = -298632863
resultInt[51] = -1109825406
resultInt[52] = -1408458269
resultInt[53] = 1776683621
resultInt[54] = 368225352
resultInt[55] = 2144908973
resultInt[56] = -1781832971
resultInt[57] = 363076002
resultInt[58] = -1418756969
resultInt[59] = -1055680967
resultInt[60] = 1820529360
resultInt[61] = 764848393
resultInt[62] = -1709589543
resultInt[63] = -944741150
resultInt[64] = 1640636603
resultInt[65] = 695895453
resultInt[66] = -1958435240
resultInt[67] = -1262539787
resultInt[68] = 1073992269
resultInt[69] = -188547518
resultInt[70] = 885444751
resultInt[71] = 696897233
resultInt[72] = 1582341984
resultInt[73] = -2015728079
resultInt[74] = -433386095
resultInt[75] = 1845853122
resultInt[76] = 1412467027
resultInt[77] = -1036647147
resultInt[78] = 375819880
resultInt[79] = -660827267
resultInt[80] = -285007387
resultInt[81] = -945834654
resultInt[82] = -1230842041
resultInt[83] = 2118290601
resultInt[84] = 887448560
resultInt[85] = -1289228135
resultInt[86] = -401779575
resultInt[87] = -1691007710
resultInt[88] = -2092787285
resultInt[89] = 511172301
resultInt[90] = -1581614984
resultInt[91] = -1070442683
resultInt[92] = 1642909629
resultInt[93] = 572466946
resultInt[94] = -2079590721
resultInt[95] = -1507123775
resultInt[96] = 708252800
resultInt[97] = -798870975
resultInt[98] = -90618175
resultInt[99] = -889489150
--------------------用长整型来存储数值的结果!---------------------
resultLong[0] = 0
resultLong[1] = 1
resultLong[2] = 1
resultLong[3] = 2
resultLong[4] = 3
resultLong[5] = 5
resultLong[6] = 8
resultLong[7] = 13
resultLong[8] = 21
resultLong[9] = 34
resultLong[10] = 55
resultLong[11] = 89
resultLong[12] = 144
resultLong[13] = 233
resultLong[14] = 377
resultLong[15] = 610
resultLong[16] = 987
resultLong[17] = 1597
resultLong[18] = 2584
resultLong[19] = 4181
resultLong[20] = 6765
resultLong[21] = 10946
resultLong[22] = 17711
resultLong[23] = 28657
resultLong[24] = 46368
resultLong[25] = 75025
resultLong[26] = 121393
resultLong[27] = 196418
resultLong[28] = 317811
resultLong[29] = 514229
resultLong[30] = 832040
resultLong[31] = 1346269
resultLong[32] = 2178309
resultLong[33] = 3524578
resultLong[34] = 5702887
resultLong[35] = 9227465
resultLong[36] = 14930352
resultLong[37] = 24157817
resultLong[38] = 39088169
resultLong[39] = 63245986
resultLong[40] = 102334155
resultLong[41] = 165580141
resultLong[42] = 267914296
resultLong[43] = 433494437
resultLong[44] = 701408733
resultLong[45] = 1134903170
resultLong[46] = 1836311903
resultLong[47] = 2971215073
resultLong[48] = 4807526976
resultLong[49] = 7778742049
resultLong[50] = 12586269025
resultLong[51] = 20365011074
resultLong[52] = 32951280099
resultLong[53] = 53316291173
resultLong[54] = 86267571272
resultLong[55] = 139583862445
resultLong[56] = 225851433717
resultLong[57] = 365435296162
resultLong[58] = 591286729879
resultLong[59] = 956722026041
resultLong[60] = 1548008755920
resultLong[61] = 2504730781961
resultLong[62] = 4052739537881
resultLong[63] = 6557470319842
resultLong[64] = 10610209857723
resultLong[65] = 17167680177565
resultLong[66] = 27777890035288
resultLong[67] = 44945570212853
resultLong[68] = 72723460248141
resultLong[69] = 117669030460994
resultLong[70] = 190392490709135
resultLong[71] = 308061521170129
resultLong[72] = 498454011879264
resultLong[73] = 806515533049393
resultLong[74] = 1304969544928657
resultLong[75] = 2111485077978050
resultLong[76] = 3416454622906707
resultLong[77] = 5527939700884757
resultLong[78] = 8944394323791464
resultLong[79] = 14472334024676221
resultLong[80] = 23416728348467685
resultLong[81] = 37889062373143906
resultLong[82] = 61305790721611591
resultLong[83] = 99194853094755497
resultLong[84] = 160500643816367088
resultLong[85] = 259695496911122585
resultLong[86] = 420196140727489673
resultLong[87] = 679891637638612258
resultLong[88] = 1100087778366101931
resultLong[89] = 1779979416004714189
resultLong[90] = 2880067194370816120
resultLong[91] = 4660046610375530309
resultLong[92] = 7540113804746346429
resultLong[93] = -6246583658587674878
resultLong[94] = 1293530146158671551
resultLong[95] = -4953053512429003327
resultLong[96] = -3659523366270331776
resultLong[97] = -8612576878699335103
resultLong[98] = 6174643828739884737
resultLong[99] = -2437933049959450366
--------------------用BigInteger来存储数值的结果!---------------------
resultBigInteger[0] = 0
resultBigInteger[1] = 1
resultBigInteger[2] = 1
resultBigInteger[3] = 2
resultBigInteger[4] = 3
resultBigInteger[5] = 5
resultBigInteger[6] = 8
resultBigInteger[7] = 13
resultBigInteger[8] = 21
resultBigInteger[9] = 34
resultBigInteger[10] = 55
resultBigInteger[11] = 89
resultBigInteger[12] = 144
resultBigInteger[13] = 233
resultBigInteger[14] = 377
resultBigInteger[15] = 610
resultBigInteger[16] = 987
resultBigInteger[17] = 1597
resultBigInteger[18] = 2584
resultBigInteger[19] = 4181
resultBigInteger[20] = 6765
resultBigInteger[21] = 10946
resultBigInteger[22] = 17711
resultBigInteger[23] = 28657
resultBigInteger[24] = 46368
resultBigInteger[25] = 75025
resultBigInteger[26] = 121393
resultBigInteger[27] = 196418
resultBigInteger[28] = 317811
resultBigInteger[29] = 514229
resultBigInteger[30] = 832040
resultBigInteger[31] = 1346269
resultBigInteger[32] = 2178309
resultBigInteger[33] = 3524578
resultBigInteger[34] = 5702887
resultBigInteger[35] = 9227465
resultBigInteger[36] = 14930352
resultBigInteger[37] = 24157817
resultBigInteger[38] = 39088169
resultBigInteger[39] = 63245986
resultBigInteger[40] = 102334155
resultBigInteger[41] = 165580141
resultBigInteger[42] = 267914296
resultBigInteger[43] = 433494437
resultBigInteger[44] = 701408733
resultBigInteger[45] = 1134903170
resultBigInteger[46] = 1836311903
resultBigInteger[47] = 2971215073
resultBigInteger[48] = 4807526976
resultBigInteger[49] = 7778742049
resultBigInteger[50] = 12586269025
resultBigInteger[51] = 20365011074
resultBigInteger[52] = 32951280099
resultBigInteger[53] = 53316291173
resultBigInteger[54] = 86267571272
resultBigInteger[55] = 139583862445
resultBigInteger[56] = 225851433717
resultBigInteger[57] = 365435296162
resultBigInteger[58] = 591286729879
resultBigInteger[59] = 956722026041
resultBigInteger[60] = 1548008755920
resultBigInteger[61] = 2504730781961
resultBigInteger[62] = 4052739537881
resultBigInteger[63] = 6557470319842
resultBigInteger[64] = 10610209857723
resultBigInteger[65] = 17167680177565
resultBigInteger[66] = 27777890035288
resultBigInteger[67] = 44945570212853
resultBigInteger[68] = 72723460248141
resultBigInteger[69] = 117669030460994
resultBigInteger[70] = 190392490709135
resultBigInteger[71] = 308061521170129
resultBigInteger[72] = 498454011879264
resultBigInteger[73] = 806515533049393
resultBigInteger[74] = 1304969544928657
resultBigInteger[75] = 2111485077978050
resultBigInteger[76] = 3416454622906707
resultBigInteger[77] = 5527939700884757
resultBigInteger[78] = 8944394323791464
resultBigInteger[79] = 14472334024676221
resultBigInteger[80] = 23416728348467685
resultBigInteger[81] = 37889062373143906
resultBigInteger[82] = 61305790721611591
resultBigInteger[83] = 99194853094755497
resultBigInteger[84] = 160500643816367088
resultBigInteger[85] = 259695496911122585
resultBigInteger[86] = 420196140727489673
resultBigInteger[87] = 679891637638612258
resultBigInteger[88] = 1100087778366101931
resultBigInteger[89] = 1779979416004714189
resultBigInteger[90] = 2880067194370816120
resultBigInteger[91] = 4660046610375530309
resultBigInteger[92] = 7540113804746346429
resultBigInteger[93] = 12200160415121876738
resultBigInteger[94] = 19740274219868223167
resultBigInteger[95] = 31940434634990099905
resultBigInteger[96] = 51680708854858323072
resultBigInteger[97] = 83621143489848422977
resultBigInteger[98] = 135301852344706746049
resultBigInteger[99] = 218922995834555169026
补充:int[]和long[] 发生溢出但并没有报错,而是给了一个错误的结果,改进方法如下。

long l =10000000000000L;
		if(l > Integer.MAX_VALUE){
			throw new Exception("超出int范围");
		}else{
			 System.out.println((int)l);
		}




你可能感兴趣的:(java)