1 + 1/2 + 1/3 +.... + 1/n 的总和,无需使用 digamma 函数和欧拉常数进行迭代

Mih*_*zac 1 c++ boost function cumulative-sum

所以我喜欢让我的生活变得艰难,我有一个任务来计算\n的总和1 + 1/2 + 1/3 + 1/4 +.... + 1/n。\n条件是不使用迭代,而是使用封闭公式。在这篇文章上:https://math.stackexchange.com/questions/3367037/sum-of-1-1-2-1-3-1-n

\n

我找到了一个看起来非常简洁的解决方案:1+1/2+1/3+\xe2\x8b\xaf+1/n=\xce\xb3+\xcf\x88(n+1)\n其中\xce\xb3 是欧拉常数,\xcf\x88 是digamma 函数。

\n

对于 digamma,我使用boost c++ 库,并使用 exp(1.0) 计算欧拉常数。

\n

问题是我没有得到正确的答案。这是我的代码:

\n
#include <iostream>\n#include <cmath>\n#include <boost/math/special_functions/digamma.hpp>\n\n\nint main(){\nint x; \nconst double g = std::exp(1.0);\n\nstd::cin >> x;\n\nstd::cout<<g + boost::math::digamma(x+1);\n\nreturn 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

提前致谢) !

\n

use*_*522 5

欧拉因有很多事物以他的名字命名而闻名。

这很容易变得令人困惑,就像这里的情况一样。

您要添加到 digamma 函数结果中的是Euler's number。您应该添加欧拉常数,这是一个以欧拉命名的不同数字。

您可以在 boost 中找到正确的数字boost::math::constants::euler,例如:

const double g = boost::math::constants::euler<double>();
Run Code Online (Sandbox Code Playgroud)

(谢谢@Eljay)


关于有多少数字是以 Leonhard Euler 的名字命名的,以及它会变得多么令人困惑,这里是维基百科页面上关于以他的名字命名的数字的部分,统计了 11 个不同的项目: https: //en.wikipedia.org/wiki/List_of_things_named_after_Leonhard_Euler #数字