Python计算阶乘的三种方法


# 递归
def func(n):
    if n == 0 or n == 1:
        return 1
    else:
        return (n * func(n - 1))


# reduce函数 + lambda函数
from functools import reduce
def fact(n):
    return reduce(lambda a, b: a * b, range(1, n + 1))


# reduce函数 + operator模块 mul函数
from functools import reduce
from operator import mul


def fact_operator(n):
    return reduce(mul, range(1, n + 1))
发布了73 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42327755/article/details/100893187