计算用户n值的e值?

时间:2014-03-02 13:10:59

标签: c

我对C编程有疑问

我想写一个程序计算e值,供用户输入n值。

你知道;我们可以为n = 1,2定义x = pow(1 + 1 / n,n)。可以在数学上示出x-> e作为n - >无穷。

我该怎么做?

我已经做到了这一点,我已经尝试但是我没有像我说的那样工作:

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

int main()
{
    int i,n,x1;

    printf("Enter a n value:");
    scanf("%d", &n);

        for (i = 0;; i++)
        {
            x1 = pow((1 + 1 / n), n);
            printf("Values:%d",x1);
        }
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

1 / n

是一个带有两个整数操作数的表达式。因此执行整数除法。如果n等于1,则评估为1。对于n大于1的所有值,此整数除法的计算结果为0

您需要浮点除法,因此必须至少使其中一个操作数成为浮点值。例如

1.0 / n

您还需要将x1声明为浮点值并使用%f。尝试用整数变量逼近e是不行的。

我想你会在某个时候需要实现一个循环终止条件。就目前而言,你的循环是没有意义的,因为循环中使用的值都没有变化。

这是一个可能正朝着正确方向前进的计划:

#include<stdio.h>
#include<math.h>

int main(void)
{
    for (int n = 1; n <= 1000; n++)
    {
        double e = pow(1 + 1.0 / n, n);
        printf("n=%d, approximation to e=%.16f\n", n, e);
    }
    printf("true value of e=%.16f\n", exp(1.0));
    return 0;
}

<强>输出

n=1, approximation to e=2.0000000000000000
n=2, approximation to e=2.2500000000000000
n=3, approximation to e=2.3703703703703698
n=4, approximation to e=2.4414062500000000
n=5, approximation to e=2.4883199999999994
n=6, approximation to e=2.5216263717421135
n=7, approximation to e=2.5464996970407121
n=8, approximation to e=2.5657845139503479
n=9, approximation to e=2.5811747917131984
n=10, approximation to e=2.5937424601000023
..........
n=991, approximation to e=2.7169116115768883
n=992, approximation to e=2.7169129915688766
n=993, approximation to e=2.7169143687840753
n=994, approximation to e=2.7169157432307069
n=995, approximation to e=2.7169171149169880
n=996, approximation to e=2.7169184838514693
n=997, approximation to e=2.7169198500421694
n=998, approximation to e=2.7169212134981109
n=999, approximation to e=2.7169225742266474
n=1000, approximation to e=2.7169239322355936
true value of e=2.7182818284590451

值得注意的是,收敛率非常低。并且估算的准确性永远不会很好,因为对于大n,您将在1.0 + 1.0 / n中受到影响。这绝对不是近似e的有用方法。

此版本使用infinite sum收敛得更快:

#include<stdio.h>
#include<math.h>

int main(void)
{
    double e = 0.0;
    double increment = 1.0;
    for (int n = 0; n <= 20; n++)
    {
        e += increment;
        increment /= (n+1);
        printf("n=%d, approximation to e=%.16f\n", n, e);
    }
    printf("true value of e=%.16f\n", exp(1.0));
    return 0;
}

<强>输出

n=0, approximation to e=1.0000000000000000
n=1, approximation to e=2.0000000000000000
n=2, approximation to e=2.5000000000000000
n=3, approximation to e=2.6666666666666665
n=4, approximation to e=2.7083333333333330
n=5, approximation to e=2.7166666666666663
n=6, approximation to e=2.7180555555555554
n=7, approximation to e=2.7182539682539684
n=8, approximation to e=2.7182787698412700
n=9, approximation to e=2.7182815255731922
n=10, approximation to e=2.7182818011463845
n=11, approximation to e=2.7182818261984929
n=12, approximation to e=2.7182818282861687
n=13, approximation to e=2.7182818284467594
n=14, approximation to e=2.7182818284582302
n=15, approximation to e=2.7182818284589949
n=16, approximation to e=2.7182818284590429
n=17, approximation to e=2.7182818284590455
n=18, approximation to e=2.7182818284590455
n=19, approximation to e=2.7182818284590455
n=20, approximation to e=2.7182818284590455
true value of e=2.7182818284590451