写一段小代码实现计算  1的1次方+2的2次方+3的3次方+…………+20的20次方,由于数据太大而使用BigInteger类型,
中间一个循环需要实现 BigInteger自加1,但是对于add
public BigInteger add(BigInteger val)这个怎么也搞不定,试了N个可能的写法都不对,还望高手不吝赐教,真的感激不尽 !
我已经写了好几个小时了,实在写不出来了,就差这一步了,谢谢
比如  BigInteger   one = new BigInteger("1");
该怎么写才能让 one自动加一
//计算小程序
//JAVA程序设计  雍俊海P11
//计算 1^1+2^2+3^3+…………+20^20
//编程时间:2009年2月28日16:03:16
import java.math.BigInteger;
  
public class CalcSum {
    BigInteger   i   =   new   BigInteger("0");      BigInteger   n   =   new   BigInteger("1");   
     BigInteger   count   =   new   BigInteger("1");   
     BigInteger   one = new BigInteger("1");  BigInteger   sum   =   new   BigInteger("0");   
  int size;
    BigInteger bi;
    BigInteger bi2;
    void CalcSum(int _i)
    {
        size = _i;
        for (int c=1;c<size+1;c++)
        {
        
        
       for( int a=1; a<c+1; a++)
           {
                      n=n.multiply(count);
//这个地方要实现 count自加1 ,就是这个地方的问题,请问如何写法;
              

           }
                            
           bi2=bi2.add(bi);
       }
       System.out.println(sum);
       }
    public static void main(String[] args)
    {
        CalcSum sum1=new CalcSum();
        sum1.CalcSum(20);
       
        
        
    }
    }

解决方案 »

  1.   

    为什么用这个呢,long不可以吗 2的63次
    int应该也可以的
      

  2.   

    void CalcSum(int _i) 

    size = _i; 
    for (int c=1;c <size+1;c++) 

     n  =  new  BigInteger(c+"");  
    for( int a=1; a <c+1; a++) 

    n=n.multiply(new  BigInteger(c+"")); 

    sum = sum.add(n);

    System.out.println(sum); 
      

  3.   

    private static double sum(int size) {
    double res = 0; for (int i = 1; i <= size; i++) {
    res += Math.pow(i, i);
    System.out.println(res);
    }
    return res;
    }
      

  4.   

    void CalcSum(int _i)
    {
    size = _i;
    for (int c=1;c <size+1;c++)
    {
    n  =  new  BigInteger(c+""); 
    for( int a=1; a <c+1; a++)
    {
    n=n.multiply(new  BigInteger(c+""));
    }
    sum = sum.add(n);
    }
    System.out.println(sum);

    //貌似这段程序有问题。
    4楼的是正解,我测试过。