[Java04] java big data class

Source: Internet
Author: User

[Java04] java big data class

When the precision of basic integers and floating-point numbers does not meet the requirements, you can use the BigInteger and BigDecimal classes provided by java.

Java. math. BigInteger

Java. math. BigInteger provides the following methods:

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 can convert a common value to a large value:

BigInteger a=BigInteger.valueOf(100);
Output febonacci Series

Output the first 100 items of the febonacci series, which is about 93 items out of the long range:

public static void  fibonacci(){        long[] a=new long[100];        for(int i=0;i<100;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("");}        }    }


Improvement: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> Public static void maid () {BigInteger [] a = new BigInteger [200]; for (int I = 0; I <200; 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 ("item" + I + ": \ t" + a [I] + "\ t ");}}

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.