Python中求阶乘(factorial)

1. math.factorial(x)

import math
value = math.factorial(x)

2. reduce函数

def factorial(n):
    return reduce(lambda x,y:x*y,[1]+range(1,n+1))

3. 递归实现

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)
posted @ 2017-09-03 15:57  清水汪汪  阅读(33674)  评论(0编辑  收藏  举报