/*
2017年3月7日16:46:47
java基础50道经典练习题 例21
Athor: ZJY
Purpose:
【程序21】
题目:求1+2!+3!+...+20!的和
程序分析:此程序只是把累加变成了累乘。
*/
public class ProgramNo21_1
{
public static void main(String[] args)
{
long sum = 0;
for (int i=1; i<21; i++) {
long subSum = 1;
for (int j=1; j<i; j++) {
subSum *= (j+1);
}
//System.out.print(subSum+" ");
sum += subSum;
}
System.out.println("前20项各数阶乘的和是: "+sum);
}
}
/*
2017年3月7日16:46:47
java基础50道经典练习题 例21
Athor: ZJY
Purpose:
*/
public class ProgramNo21_2
{
public static void main(String[] args){
long sum = 0;
for(int i=0; i<20; i++)
sum += factorial(i+1);
System.out.println(sum);
}
//阶乘
private static long factorial(int n){
long mult = 1; //zjy (int)
for(int i=1; i<n+1; i++)
mult *= i;
//System.out.print(mult+" ");
return mult;
}
}