# 递归
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))
Python计算阶乘的三种方法
猜你喜欢
转载自blog.csdn.net/qq_42327755/article/details/100893187
今日推荐
周排行