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

Sum of 1 + 1/2 + 1/3 +.... + 1/n without iterations using digamma function and Euler's constant

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

我找到了一个非常漂亮的解决方案:1+1/2+1/3+⋯+1/n=γ+ψ(n+1) 其中 γ 是欧拉常数,ψ 是双伽玛函数。

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

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

#include <iostream>
#include <cmath>
#include <boost/math/special_functions/digamma.hpp>


int main(){
int x; 
const double g = std::exp(1.0);

std::cin >> x;

std::cout<<g + boost::math::digamma(x+1);

return 0;
}

提前致谢)!

欧拉因有很多东西以他的名字命名而闻名。

这很容易让人感到困惑,就像这里的情况一样。

您添加到双伽玛函数结果的是欧拉数。您应该添加 欧拉常数,这是一个以欧拉命名的不同数。

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

const double g = boost::math::constants::euler<double>();

(感谢@Eljay)


有关以 Leonhard Euler 命名的数量及其令人困惑程度的一些上下文,这里是维基百科页面的部分,仅以 numbers 以他命名,共计 11 个不同的项目:https://en.wikipedia.org/wiki/List_of_things_named_after_Leonhard_Euler#Numbers