n的阶乘求和 python
时间: 2023-11-30 14:42:33 浏览: 204
n的阶乘 python代码
以下是两种Python计算n的阶乘求和的方法:
1.使用递归函数计算n的阶乘和
```python
import numpy as np
import pandas as pd
from numpy import *
def Recursive(n):
n = int(n)
sum = 0
chengji = 1
for i in range(1,n+1):
chengji = chengji*i
sum += chengji
print('%d! 的和是 %d ' % (n, sum))
if __name__ == '__main__':
print("这个程序是计算n阶阶乘和")
num = input("请输入阶数:")
Recursive(num)
```
2.使用循环和递归函数计算n的阶乘和
```python
def fac(m):
if m==0:
return 1
else:
return (m*fac(m-1))
n=int(input())
s=0
for i in range(1,n+1):
s +=fac(i)
print(s)
```
阅读全文