`
xitongyunwei
  • 浏览: 926244 次
文章分类
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics