'''
#不使用函数递归,实现一个阶乘计算函数(n<=170)
#10!=10*9*8*7*6*5*4*3*2*1
求阶乘的几种方法:
自己写:
1、循环
2、递归
3、借助内置函数 eval()
使用 内置模块的函数:
1、value = math.factorial(n)
2、value = functools.reduce(lambda x,y:x*y, range(1,n+1))
'''
import math
import functools
def fact(n=1):
nr = ""
for i in range(1,n+1):
nr += "*"+str(i)
ns = nr.strip("*")
nss = eval(ns)
print(f"{ns}={nss}")
return nss
def fact2(n):
nr = ""
for i in range(1,n+1):
nr += "*"+str(i)
return eval(nr.strip("*"))
def fact3(n):
nr = 1
for i in range(1,n+1):
nr = nr*i
return nr
def fact4(n,nr=1):
if n-1 ==0:
return nr
else:
return fact4(n-1,nr*n)
def fact5(n):
L = [i for i in range(1,n+1)]
ret = functools.reduce(lambda x,y:x*y,L)
return ret
def fact6(n):
value = math.factorial(n)
return value
_initial_missing = object()
def reduce(function, sequence, initial=_initial_missing):
it = iter(sequence)
print(type(initial),type(_initial_missing))
if initial is _initial_missing:
try:
value = next(it)
except StopIteration:
raise TypeError("reduce() of empty sequence with no initial value") from None
else:
value = initial
for element in it:
value = function(value, element)
return value
# ret = reduce(lambda x,y:x*y,[1,2,3])
# ret = fact(n=10)
# ret = fact2(10)
# ret = fact3(10)
# ret = fact4(10)
# ret = fact5(10)
ret = fact6(10)
print(ret)