输入一个自然数n,求表达式 f(n) = 1!*2!*3!*.....*n! 的结果?
示例代码:
from functools import reduce
def sums(n):
if n == 1:
return 1
else:
return reduce(lambda x, y: x * y, range(1, n + 1)) * sums(n - 1)
num = int(input('请输入一个n:'))
result = sums(num)
print(result)
运行结果:
输入一个自然数n,求表达式 f(n) = 1!+2!+3!+.....+n! 的结果?
示例代码:
from functools import reduce
def sums(n):
if n == 1:
return 1
else:
return reduce(lambda x, y: x * y, range(1, n + 1)) + sums(n - 1)
num = int(input('请输入一个n:'))
result = sums(num)
print(result)
运行结果: