The BigInteger classes and BigDecimal classes provided by Java can be used when the primary integer and floating-point precision do not meet the requirements.
Java.math.BigInteger
Java.math.BigInteger provides the method:
- BigInteger Add (BigInteger Other)
- BigInteger Subtract (BigInteger other)
- BigInteger Multiply (BigInteger other)
- BigInteger Divide (Bigintegr other)
- int CompareTo (BigInteger other)
- BigInteger MoD (BigInteger Other)
Static BigInteger valueOf (long x)
The valueof method converts a normal value to a large value:
a=BigInteger.valueOf(100);
Output Febonacci Series
Output Febonacci The first 100 items of the series. About 93 items will exceed the range of long:
Public Static void Fibonacci(){Long[] A=New Long[ -]; for(intI=0;i< -; i++) {if(i==0) {a[0]=0; System. out. Print (a[i]+"\ T");Continue; }Else if(i==1) {a[1]=1; System. out. Print (a[i]+"\ T");Continue; } a[i]=a[i-1]+a[i-2]; System. out. Print (a[i]+"\ T");if((i+1)%5==0) {System. out. println ("");} } }
Improved:
Public Static void Fibonacci() {biginteger[] a =Newbiginteger[ $]; for(inti =0; I < $; i++) {if(i = =0) {a[0] = biginteger.valueof (0); System. out. print (A[i] +"\ T");Continue; }Else if(i = =1) {a[1] = biginteger.valueof (1); System. out. print (A[i] +"\ T");Continue; } A[i] = A[i-1].add (A[i-2]); System. out. println ("section"+ i +"item: \ t"+ A[i] +"\ T"); } }
Java large number class