编程语言
首页 > 编程语言> > python 实现阶乘计算的三种方式

python 实现阶乘计算的三种方式

作者:互联网

def jiecheng(num,result):
    if num>1:
        result = num*result
        num-=1
        jiecheng(num,result)

    else:
        print('result=', result)

# jiecheng(5, 1)

def jiecheng2(num,result):
    for i in range(1,num+1):
        # print(i)
        result*=i
    print('result=',result)

# jiecheng2(5,1)

from functools import reduce
def jiecheng3(num):
    result = reduce(lambda x,y:x*y,range(1,num+1))
    print(result)

# jiecheng3(5)

标签:jiecheng2,python,print,num,三种,result,阶乘,jiecheng,def
来源: https://blog.csdn.net/CXY00000/article/details/122397713