---
title: "1-20的阶乘之和（java）"
author: "mywebcode"
date: "2012-12-18T12:59:00Z"
tags: []
slug: "1-20-java"
draft: false
lastmod: "2014-04-19T16:41:24Z"
id: 2049485
url: "https://mywebcode.iteye.com/blog/2049485"
---

import java.math.BigInteger;

public class Factorial {  
//2)求1！+2！+……+20！  
public static void main(String\[] args){  
BigInteger sum=BigInteger.ZERO;  
for(BigInteger i=BigInteger.ONE;i.intValue()&lt;=20;){  
i=i.add(BigInteger.ONE);  
sum=sum.add(factorial(i));  
}  
System.out.println(sum.toString());  
}

public static BigInteger factorial(BigInteger bigInteger){  
if(bigInteger.intValue()==1){  
return BigInteger.ONE;  
}  
else  
return bigInteger.multiply(factorial(bigInteger.subtract(BigInteger.ONE)));  
}  
}

结果：53652269665821260312