编写函数,求“1/(1*2)-1/(2*3)+1/(3*4)-1/(4*5)+……”前n项的和,将n作为参数,n由用户输入
时间: 2024-06-14 09:03:29 浏览: 82
以下是求“1/(1*2)-1/(2*3)+1/(3*4)-1/(4*5)+……”前n项的和的Python函数代码,其中n为用户输入的参数:
```python
def fun(n):
sum = 0
b = -1
c = -1
for i in range(1, n+1):
c = c * -1
b = b * c
sum = sum + b * (1 / (i * (i+1)))
return sum
```
阅读全文