Java large number class

Source: Internet
Author: User

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:

    1. BigInteger Add (BigInteger Other)
    2. BigInteger Subtract (BigInteger other)
    3. BigInteger Multiply (BigInteger other)
    4. BigInteger Divide (Bigintegr other)
    5. int CompareTo (BigInteger other)
    6. BigInteger MoD (BigInteger Other)
    7. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.