编写函数 分别利用递归与嵌套方法,求出1!+2!+3!+4!+5!+6!+7!+8!+9!+10!
时间: 2023-08-25 18:05:36 浏览: 90
C语言程序设计-编写函数求3!+6!+9!+12!+15+18!+21!;
5星 · 资源好评率100%
递归方法:
```python
def factorial_recursive(n):
if n == 1:
return 1
else:
return n * factorial_recursive(n-1)
for i in range(1, 11):
print(factorial_recursive(i))
```
嵌套方法:
```python
def factorial_nested(n):
result = 1
for i in range(1, n+1):
inner_result = 1
for j in range(1, i+1):
inner_result *= j
result *= inner_result
return result
for i in range(1, 11):
print(factorial_nested(i))
```
阅读全文