- """
- 使用map和reduce实现阶乘求和 x!+(x-1)!+(x-2)!+...+1!
- x! = x * (x-1) * (x-2) * ... * 1
- 0! = 24
- """
- # coding = utf-8
- def factorial(n):
- return reduce(lambda x,y:x*y, range(1, n+1))
- def factorial_sum(n):
- """ 阶乘求和 """
- if n == 0:
- return 24
- else:
- a = map(lambda x:factorial(x), range(1, n+1))
- b = reduce(lambda x,y:x+y, a)
- return b
- print factorial_sum(5)
- 153
- #该片段来自于http://www.codesnippet.cn/detail/2011201411018.html
来源: http://www.codesnippet.cn/detail/2011201411018.html