fact1.py 895 字节
Newer Older
F
feilong 已提交
1 2 3 4 5
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数
# 描述:循环计算阶乘函数

F
feilong 已提交
6 7
def fact1(n):
    r = 1
F
feilong 已提交
8
    for i in range(0, n):
F
feilong 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        r *= (i+1)
    return r


def fact2(n):
    r = 1
    while (n > 0):
        r *= n
        n -= 1
    return r


def fact3(op):
    import math
    z = op + 1
    p = [1.000000000190015, 76.18009172947146, -86.50532032941677,
         24.01409824083091, -1.231739572450155, 1.208650973866179E-3, -5.395239384953E-6]

    d1 = math.sqrt(2 * math.pi) / z

    i = 1
    d2 = p[0]
    while i <= 6:
        d2 += p[i] / (z + i)
        i += 1

    d3d4 = math.pow((z + 5.5), (z + 0.5))*math.exp(-(z + 5.5))
    d = d1 * d2 * d3d4
    return int(d)


def fact4(n):
    import math
    r = math.factorial(n)
    return r
F
feilong 已提交
44 45 46


if __name__ == '__main__':
F
feilong 已提交
47 48 49 50
    print(fact1(10))
    print(fact2(24))
    print(fact3(24))
    print(fact4(10))