求1到5阶乘之和

1 方法一:
2 s = 1
3 sum = 0
4 for i in range(1,6):
5     s = s*i
6     sum += s
7 print(s)
8 print(sum)
1 方法二:效率低,每次都要重新算k
2 
3 n = 0 
4 for i in range(1,6):
5     k = 1
6     for j in range(1, i + 1):
7         k = k * j
8     n = n + k
9 print(n)

 

posted @ 2018-08-01 22:50  JerryZao  阅读(3722)  评论(0编辑  收藏  举报