how can I calculate euler constant
22 views (last 30 days)
Show older comments
colud you please help me to find the correct code to find eulers constant (20 digits after decimal) γ=0.57721 56649 01532 86061
0 Comments
Answers (3)
Sam Chak
on 21 Oct 2024
Edited: Sam Chak
on 21 Oct 2024
Hi @hajer
Three days ago, @John D'Errico and I discussed the Gamma function in this thread. I’m not sure how to perform the numerical integration shown in the image at first glance, but I do recognize the uppercase gamma as the Gamma function. Here’s the symbolic approach. I’m sure others will also share different approaches, including the limit of the infinite sum.
syms x
dG = diff(gamma(x))
EulerConstant = vpa(subs(-dG, x, 1))
Result:
1 Comment
John D'Errico
on 21 Oct 2024
Edited: John D'Errico
on 21 Oct 2024
Interesting, in that there are quite a few formulas to compute the Euler (or Euler-Mascheroni) constant shown here.
Though as I recall, not all of them will be easy to compute in any reasonable time, certainly not when 20 digits are needed. Your solution is correct of course, and does yield all correct digits.
The form I chose some years ago when I wanted to compute the Euler constant was one employing Harmonic numbers, which had the virtue of not needing a symbolic gamma function available.
R
on 21 Oct 2024
If you’re looking to calculate Euler’s constant, there’s already a well-formed answer available on the forum that you can refer to: https://www.mathworks.com/matlabcentral/answers/267362-how-to-find-euler-s-constant?s_tid=answers_rc1-1_p1_MLT
Based on the information provided in that answer, here’s a MATLAB snippet that can help you achieve the same result:
% Set the number of terms for the series
n = 1e10;
% Initialize the sum
euler_sum = 0;
% Calculate the sum of the series
for k = 1:n
euler_sum = euler_sum + 1/k;
end
% Calculate Euler's constant
gamma = euler_sum - log(n);
% Display the result with high precision
fprintf('Euler''s constant (20 digits after decimal): %.20f\n', gamma);
Increasing the number of terms will yield a more accurate result, but it will also significantly increase the computational time required. For even higher precision, you might want to explore more advanced methods, such as the Bessel function method mentioned in that answer.
Hope this helps!
2 Comments
John D'Errico
on 21 Oct 2024
Edited: John D'Errico
on 21 Oct 2024
That does NOT give you 20 correct digits after the decimal!!!!!!!
As performed in double precision, it can NEVER do so!!!!!!!
Why not? because double precision will not yield 20 correct decimal digits in such a computation. And even if it could, the simple sum will need to iterate at least more than 1e20 terms in the sum!
If your computer could process that loop at a rate of 1e9 loops per second, that still would then require 1e11 seconds to terminate. Since there are roughly 30e6 seconds in a year
number_of_years_until_termination = 1e11/30e6
So as a LOWER bound on the amount of time to terminate will be around 3333 years.
R
on 21 Oct 2024
Edited: R
on 21 Oct 2024
Thanks for your feedback @John D'Errico! I appreciate your insights about double precision limitations and the impracticality of summing n = 1e10 terms. Even after using the loop and vpa method, I found that I still got an inaccurate value for Euler's constant. It seems that the method suggested by @Sam Chak, which involves using the symbolic gamma function, is indeed the better approach for achieving the desired accuracy. I'll look into that for more reliable results. Thanks again!
Edit: Answer by Torsten is the best so far:
g = vpa(eulergamma(), 20)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!