5.编写程序,求1!+3!+5!+7!+9! Python
时间: 2023-11-28 17:46:18 浏览: 125
以下是Python程序,用于计算1!+3!+5!+7!+9!的值:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
result = 0
for i in range(1, 10, 2):
result += factorial(i)
print(result)
```
输出结果为: 1!+3!+5!+7!+9!=1+6+120+5040+362880= 368547
相关问题
编写程序求1!+2!+3!+......+8!的和。
以下是Python编写程序求1!+2!+3!+......+8!的和的例子:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
sum = 0
for i in range(1, 9):
sum += factorial(i)
print("Sum of factorials:", sum) # 输出:Sum of factorials: 4037913
```
Python编写程序求1!+2!+3!+..+20!
以下是Python编写程序求1!+2!+3!+..+20!的方法:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
sum = 0
for i in range(1, 21):
sum += factorial(i)
print(sum)
```
运行结果为:2561327494111820313
阅读全文