How to resolve the algorithm Euler's constant 0.5772... step by step in the Ruby programming language
How to resolve the algorithm Euler's constant 0.5772... step by step in the Ruby programming language
Table of Contents
Problem Statement
Compute the Euler constant 0.5772... Discovered by Leonhard Euler around 1730, it is the most ubiquitous mathematical constant after pi and e, but appears more arcane than these. Denoted gamma (γ), it measures the amount by which the partial sums of the harmonic series (the simplest diverging series) differ from the logarithmic function (its approximating integral): lim n → ∞ (1 + 1/2 + 1/3 + … + 1/n − log(n)). The definition of γ converges too slowly to be numerically useful, but in 1735 Euler himself applied his recently discovered summation formula to compute ‘the notable number’ accurate to 15 places. For a single-precision implementation this is still the most economic algorithm. In 1961, the young Donald Knuth used Euler's method to evaluate γ to 1271 places. Knuth found that the computation of the Bernoulli numbers required in the Euler-Maclaurin formula was the most time-consuming part of the procedure. The next year Dura Sweeney computed 3566 places, using a formula based on the expansion of the exponential integral which didn't need Bernoulli numbers. It's a bit-hungry method though: 2d digits of working precision obtain d correct places only. This was remedied in 1988 by David Bailey; meanwhile Richard Brent and Ed McMillan had published an even more efficient algorithm based on Bessel function identities and found 30100 places in 20 hours time. Nowadays the old records have far been exceeded: over 6·1011 decimal places are already known. These massive computations suggest that γ is neither rational nor algebraic, but this is yet to be proven.
[1] Gourdon and Sebah, The Euler constant γ. (for all formulas) [2] Euler's original journal article translated from the latin (p. 9)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Euler's constant 0.5772... step by step in the Ruby programming language
This Ruby code snippet calculates the sum of the reciprocals of the first n natural numbers, then subtracts the natural logarithm of n from that sum, and prints the result. Let's break down the code line by line:
-
n = 1e6: This line assigns the value 1 million (1e6) to the variablen. -
(1..n).sum: This expression creates a range of numbers from 1 to n (inclusive) using the range operator.., and then calculates the sum of all the numbers in that range using thesummethod. In this case, the result is the sum of the first 1 million natural numbers. -
{ 1.0/_1 }: This block of code specifies what operation should be performed on each element of the range. Here, it divides 1.0 by the current element of the range (_1) for each element in the range. So, it calculates the sum of the reciprocals of the first n natural numbers. -
- Math.log(n): This expression subtracts the natural logarithm of n from the sum calculated in the previous step. The natural logarithm is calculated using theMath.logfunction. -
p: Finally, thepmethod is used to print the result.
The output of this code is approximately 14.3927. This value is known as the Euler-Mascheroni constant, which is the limiting difference between the harmonic mean and the natural logarithm of n as n approaches infinity.
Source code in the ruby programming language
n = 1e6
p (1..n).sum{ 1.0/_1 } - Math.log(n)
You may also check:How to resolve the algorithm Command-line arguments step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sutherland-Hodgman polygon clipping step by step in the Ruby programming language
You may also check:How to resolve the algorithm Stirling numbers of the first kind step by step in the Ruby programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Ruby programming language
You may also check:How to resolve the algorithm Fermat numbers step by step in the Ruby programming language