在 C++ 中计算欧拉数
编写一个程序来计算欧拉数 e。为此,首先编写一个接受参数 n 并返回结果 (1+1/n)n 的函数。当 n 接近无穷大时,该函数的极限接近 e。在主程序中,编写一个循环,以递增的 n 值调用此函数。在每次迭代中,将 n 乘以 2(如果每次只将 1 加到 n,算法将无法工作),并在新的近似值与之前的近似值相差小于 1e-8 时停止。现在你已经有了一个很好的估计。因此,在 main() 中,打印您的最佳近似值以及生成它的数字 n。
我已经完成了 for 循环之前的所有操作。我不太明白在新的和以前的数字大致相同之后我应该如何停止 for 循环。
这是我的函数:
double euler_finder(double n)
{
return pow((1+1/n), n);
}
这是我在 main 方法中的 for 循环,其中 ???是我遇到问题的地方:
for (int i=0; i<????; i*=2) {
}
编辑:解决方案已发布,所以这里的样子:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double euler(int n);
int main()
{
int trialN = 4;
double guess1, guess2;
guess1 = euler(1);
guess2 = euler(2);
while( abs(guess1-guess2) > 1e-8 )
{
cout<< trialN << " " << guess2<<endl;
guess1 = guess2;
guess2 = euler( trialN );
trialN*=2;
}
cout<<setprecision(8)<<"e is approximately "<<guess2<<" and we got it with a value of ";
cout<<trialN<<" for n";
return 0;
}
double euler(int n)
{
return pow((1+1.0/n), n);
}
输出:
4 2.25
8 2.44141
16 2.56578
32 2.63793
64 2.67699
128 2.69734
256 2.70774
512 2.71299
1024 2.71563
2048 2.71696
4096 2.71762
8192 2.71795
16384 2.71812
32768 2.7182
65536 2.71824
131072 2.71826
262144 2.71827
524288 2.71828
1048576 2.71828
2097152 2.71828
4194304 2.71828
8388608 2.71828
16777216 2.71828
33554432 2.71828
67108864 2.71828
134217728 2.71828
e is approximately 2.7182818 and we got it with a value of 268435456 for n
Write a program that calculates Euler’s number e. To do this, first write a function that takes a parameter n, and returns the result (1+1/n)n. The limit of this function approaches e as n approaches infinity. In your main program, write a loop that calls this function with increasing values of n. On each iteration, multiply n by 2 (if you just add 1 to n each time, the algorithm won' work) and stop when the your new approximation and your previous approximation differ by less than 1e-8. Now you have a pretty good estimate. So in main(), print your best approximation and the number n that generated it.
I have done everything up until the for-loop. I don't quite understand how am I supposed to stop for-loop after new and previous numbers are approximately the same.
Here is my function:
double euler_finder(double n)
{
return pow((1+1/n), n);
}
And here is my for-loop in the main method, where ??? is where I'm having a problem:
for (int i=0; i<????; i*=2) {
}
EDIT: The solution has been posted so here how it looks like:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double euler(int n);
int main()
{
int trialN = 4;
double guess1, guess2;
guess1 = euler(1);
guess2 = euler(2);
while( abs(guess1-guess2) > 1e-8 )
{
cout<< trialN << " " << guess2<<endl;
guess1 = guess2;
guess2 = euler( trialN );
trialN*=2;
}
cout<<setprecision(8)<<"e is approximately "<<guess2<<" and we got it with a value of ";
cout<<trialN<<" for n";
return 0;
}
double euler(int n)
{
return pow((1+1.0/n), n);
}
Output:
4 2.25
8 2.44141
16 2.56578
32 2.63793
64 2.67699
128 2.69734
256 2.70774
512 2.71299
1024 2.71563
2048 2.71696
4096 2.71762
8192 2.71795
16384 2.71812
32768 2.7182
65536 2.71824
131072 2.71826
262144 2.71827
524288 2.71828
1048576 2.71828
2097152 2.71828
4194304 2.71828
8388608 2.71828
16777216 2.71828
33554432 2.71828
67108864 2.71828
134217728 2.71828
e is approximately 2.7182818 and we got it with a value of 268435456 for n
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要忘记有多个循环结构可用;
do ... while ()
循环可能会是这个特定算法的更好选择。 (是的,我确信您可以使其适合for
循环,但它可能读起来不太清楚。)Don't forget that there are multiple loop constructs available; a
do ... while ()
loop would probably be a better choice for this specific algorithm. (Yes, I'm sure you could make it fit into afor
loop, but it might not read as clearly.)它没有说使用 for 循环。你可以,尽管我认为 while 或 do-while 更具可读性。无论哪种方式,您都需要一个条件,例如:
编辑:已修复。严格来说,应该是
>=
。编辑2:您可以从人工
prevValue
和value
开始,例如 0 和 1,或者使用 do-while (如上所述)。It doesn't say to use a for loop. You can, though I consider a while or do-while more readable. Either way, you need a condition like:
EDIT: Fixed. Strictly, it should be
>=
.EDIT 2: You can start with artificial
prevValue
andvalue
, such as 0 and 1, or use a do-while (as mentioned).首先,如果 i 从 0 开始,然后继续乘以 2,它不会前进很多。从 1 开始。
其次,根据问题陈述,当你的近似值足够好时,你应该停止。所以循环停止条件不在i上,而是abs(proper_E - your_approximation) > 1e-8
First of all, if i starts off at 0, and you keep multiplying by 2, it won't advance very far. Start off at 1.
Second of all, according to the problem statement, you should stop when your approximation is good enough. So the loop stop condition is not on i, but rather abs(proper_E - your_approximation) > 1e-8