1!+2!···+20!阶乘的和!

来源:互联网 发布:游戏机禁令 知乎 编辑:程序博客网 时间:2024/06/03 07:21

1!+2!···+20!阶乘的和!

引用BigInterger 类·(处理大数字运算)

使用文档-http://www.yq1012.com/api/java/math/BigInteger.html

import java.math.BigInteger;

代码如下


public class Big {
    public static void main(String args[]){
        BigInteger sum= BigInteger.ZERO;//定义总和初始值为0
        for(BigInteger i=BigInteger.ONE;i.intValue()<20;){//循环条件
            i=i.add(BigInteger.ONE);//递增+1,直到值为20 ;add(this+val)
            sum=sum.add(fac(i));//sum+val
        }
        System.out.println(sum.toString());
    }
    public  static BigInteger fac(BigInteger bigNum){
        if(bigNum.intValue()==1){//如果为1返回
            return BigInteger.ONE;
        }
        else{
            return bigNum.multiply(fac(bigNum.subtract(BigInteger.ONE)));bigNum的阶乘;
        }
        
    }

}
1 0
原创粉丝点击