使用map和reduce实现阶乘求和

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

"""
使用map和reduce实现阶乘求和 x!+(x-1)!+(x-2)!+...+1!
x! = x * (x-1) * (x-2) * ... * 1
0! = 1
"""

# 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 1
    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