gpt4 book ai didi

计算用户n值的e值?

转载 作者:行者123 更新时间:2023-11-30 15:37:40 24 4
gpt4 key购买 nike

我有一个关于 C 编程的问题

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

你知道;我们可以定义 x=pow(1+1/n,n) 对于 n=1,2.. 可以从数学上证明 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 / n

是具有两个整数操作数的表达式。如此执行整数除法。如果 n 等于 1,则其计算结果为 1。对于大于 1 的所有 n 值,此整数除法的计算结果为 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.0000000000000000n=2, approximation to e=2.2500000000000000n=3, approximation to e=2.3703703703703698n=4, approximation to e=2.4414062500000000n=5, approximation to e=2.4883199999999994n=6, approximation to e=2.5216263717421135n=7, approximation to e=2.5464996970407121n=8, approximation to e=2.5657845139503479n=9, approximation to e=2.5811747917131984n=10, approximation to e=2.5937424601000023..........n=991, approximation to e=2.7169116115768883n=992, approximation to e=2.7169129915688766n=993, approximation to e=2.7169143687840753n=994, approximation to e=2.7169157432307069n=995, approximation to e=2.7169171149169880n=996, approximation to e=2.7169184838514693n=997, approximation to e=2.7169198500421694n=998, approximation to e=2.7169212134981109n=999, approximation to e=2.7169225742266474n=1000, approximation to e=2.7169239322355936true value of e=2.7182818284590451

It's quite interesting to note that the rate of convergence is really poor. And the accuracy of the estimate can never be good because for large n you will suffer round off in 1.0 + 1.0 / n. This is absolutely not a useful way to approximate e.

This version, using an infinite sum, converges much more rapidly:

#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.0000000000000000n=1, approximation to e=2.0000000000000000n=2, approximation to e=2.5000000000000000n=3, approximation to e=2.6666666666666665n=4, approximation to e=2.7083333333333330n=5, approximation to e=2.7166666666666663n=6, approximation to e=2.7180555555555554n=7, approximation to e=2.7182539682539684n=8, approximation to e=2.7182787698412700n=9, approximation to e=2.7182815255731922n=10, approximation to e=2.7182818011463845n=11, approximation to e=2.7182818261984929n=12, approximation to e=2.7182818282861687n=13, approximation to e=2.7182818284467594n=14, approximation to e=2.7182818284582302n=15, approximation to e=2.7182818284589949n=16, approximation to e=2.7182818284590429n=17, approximation to e=2.7182818284590455n=18, approximation to e=2.7182818284590455n=19, approximation to e=2.7182818284590455n=20, approximation to e=2.7182818284590455true value of e=2.7182818284590451

关于计算用户n值的e值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22127985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com