java 1的阶乘之和_1-20的阶乘之和(java)
生活随笔
收集整理的這篇文章主要介紹了
java 1的阶乘之和_1-20的阶乘之和(java)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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()<=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
分享到:
2012-12-18 12:59
瀏覽 319
評論
總結
以上是生活随笔為你收集整理的java 1的阶乘之和_1-20的阶乘之和(java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql varchar(20)_My
- 下一篇: mysql架构深入_mysql性能优化2