编写函数fun,功能是计算如下多项式:s=1+x+x**2/2!+x**3/3!+•••+x**n/n!
时间: 2023-11-03 13:01:44 浏览: 188
Sure! 下面是函数fun的代码,可以计算给定n和x下的多项式值:
```python
import math
def fun(n, x):
s = 0
for i in range(1, n+1):
s += (x ** i) / math.factorial(i)
return s
```
希望能够帮到您!如果您还有其他问题,欢迎随时提出。
相关问题
编写函数fun,计算下列多项式:s=1+x+x**2/2!+x**3/3!+•••+x**n/n!
Sure, I can help you with that! Here is the function:
```python
def fun(x, n):
s = 1
fact = 1
for i in range(1, n+1):
fact *= i
s += (x ** i) / fact
return s
```
Simply pass in the value of `x` and the number of terms `n` you want to use in the approximation. The function will return the value of the sum of the series.
Hope this helps!
/*请编写函数fun,其功能是计算并输出如下多项式的值: Sn=l+1/1!+1/2!+1/3!+1/4!+…+1/n!
*/#include <stdio.h>
double fun(int n)
{
double result = 0, factorial = 1;
int i;
for (i = 1; i <= n; i++) {
factorial *= i;
result += 1 / factorial;
}
return result;
}
int main()
{
int n;
printf("请输入n的值:");
scanf("%d", &n);
printf("Sn的值为:%lf\n", fun(n));
return 0;
}
阅读全文