小练习---阶乘

  • 求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)

 

 

posted @ 2020-03-25 21:36  小他_W  阅读(126)  评论(0编辑  收藏  举报