求输入数字的阶乘 及加和 #s=1!+2!+3!+…..+n!

#s=1!+2!+3!+…..+n!

from functools import reduce

def factorial(n):
    result=0
    for i in range(1,n+1):
        result+=reduce(lambda x,y:x*y,list(range(1,i+1)))
        print('%s的阶乘加和结果为:%s'%(i,reduce(lambda x,y:x*y,list(range(1,i+1)))))
    return result

print('1到%s的阶乘加和结果是:%s'%(5,factorial(5)))

#方法2
def
factorial(n): result=0 for i in range(1,n+1): rr=1 for j in range(1,i+1): rr*=j result+=rr print('%s的阶乘加和结果为:%s'%(i,rr)) return result print('1到%s的阶乘加和结果是:%s'%(5,factorial(5))) print('1到%s的阶乘加和结果是:%s'%(4,factorial(4)))

 

 

posted @ 2018-12-29 14:04  小金儿  阅读(675)  评论(0编辑  收藏  举报