首页 > 代码库 > c - 计算1到20的阶乘

c - 计算1到20的阶乘

 1 #include <stdio.h> 2  3 /* 4 题目:求 1+2!+3!+...+20!的和 5 */ 6 unsigned long long int 7 factorial(long n) { 8     unsigned long long int tmp = 1;    //每一个数的阶乘,如,1!,2!,3!...20!. 9     unsigned long long int sum = 0;    //总和,即1+2!+3!+...+20!的和.10     for(long long int i = 1; i <= n; i++)     {11         tmp *= i;12         sum += tmp;13     }14 15     return sum;16 }17 18 int19 main(void) {20     unsigned long long int r = factorial(20);21     printf("%llu\n", r);        22 23 /*24 _int64 fact = 1,i,sum = 0;25     for(i = 1; i < 21; ++i)    {26         fact *= i;27         sum += fact;28     }29     printf("sum = %I64d\n",sum);30 */31 }

 

c - 计算1到20的阶乘