java中的大整数类BigInteger处理大整数——小小钟

来源:互联网 发布:mac aria2gui 下载 编辑:程序博客网 时间:2024/05/16 07:57

在很多时候,我们都要处理很大的整数,在java中有一个专门处理大整数的类BigInteger

我用个例子来说说:
/*1^1+2^2+3^3+4^4+...+1000^1000的后10位数*/import java.math.BigInteger;public class Test01 {public static void main(String[] args) {test01(1000);}public static void test01(long n){BigInteger bp = null;BigInteger bsum = new BigInteger(0+"");BigInteger ba = new BigInteger(1+"");for(int i=1;i<=n;i++){bp = new BigInteger(i+"");ba = bp.pow(i);bsum = bsum.add(ba);}System.out.println(bsum);String strSum = bsum+"";System.out.println(strSum.substring(strSum.length()-10));}}

原创粉丝点击