小练习---阶乘
- 求1+2!+3!+...+20!的和
- 解法一
1 n = 0 2 s = 0 3 t = 1 4 for n in range(1,21): 5 t *= n 6 s += t 7 print('1! + 2! + 3! + ... + 20! = %d' % s)
1! + 2! + 3! + ... + 20! = 2561327494111820313
- 解法二
1 s = 0 2 l = range(1,21) 3 def op(x): 4 r = 1 5 for i in range(1,x+1): 6 r *= i 7 return r 8 s = sum(map(op,l)) 9 print('1! + 2! + 3! + ... + 20! = %d' % s)
正是江南好风景