如何通过无穷级数循环常数e

问题描述 投票:-1回答:2

𝒆的值由无穷级数给出𝒆=𝟏+𝟏/𝟏!+𝟏/𝟐!+𝟏/𝟑!+𝟏/𝟒!+⋯上述算法在计算上是有效的。用它来计算𝒆到任意精度。

如何在公式中输入公式?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    double e;
    double n;
    float i;
    n = atof(argv[1]);
    e = 1;
    for (I = 0; I < n; i++)
    e = 1 + 1/n!;
    printf("%.10f\n", e);
    return 0;
}
c for-loop
2个回答
1
投票
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { double e = 1.0, f = 1.0; // f is your 'accumulated' factorial int i, n = atoi(argv[1]); // Best to use integers for loop counting for (i = 1; i < n; i++) { f *= i; // First time, this wil be 1 * 1 = 1; second time 2, third 6, etc e += 1.0/f; // Divide here by our 'running' value for i! } printf("%.10f\n", e); return 0; }

0
投票
long long factorial(int n){ long long fact=1; for(i=2;i<=;i++) fact*=1; return fact; }

对于累积总和,您应该使用e = e + 1 + 1 / n!你可以计算出n!使用上述阶乘函数。如果您有任何疑问,请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.