Python 阶乘
Factorial
0!=1 1!=1 2!=1*2 3!=1*2*3 4!=1*2*3*4 ... for def factorial(n): p=1 for i in range(1,n): p*=i return p print(factorial(5)) 120 for递归 def factorial(n,p=1): if n==1: return p p*=n return factorial(n-1,p) print(factorial(6)) 720 def factorial(n,p=1): return p if n<2 else factorial(n-1,p*n) print(factorial(5)) 公式法 def factorial(n): if n< 2: return n return n*factorial(n-1) print(factorial(5)) 120
Posted by BlueFat on Sunday, August 14, 2022