How to resolve the algorithm Euler's constant 0.5772... step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Euler's constant 0.5772... step by step in the Python 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 Python programming language

The provided Python code calculates Euler's constant (also known as the Euler-Mascheroni constant) using Euler's Zeta Series. Here's how it works:

  1. Importing Libraries:
from scipy import special as s

This line imports the special submodule from the scipy library, which provides functions for mathematical operations, including the Zeta function used in this code.

  1. Function Definition:
def eulers_constant(n):

The code defines a function called eulers_constant that takes a positive integer n as its argument. This function will calculate and return an approximation of Euler's constant.

  1. Initialization:
   k = 2
   euler = 0

Inside the function, two variables are initialized:

  • k is set to 2, which represents the starting value of the loop to calculate the series.
  • euler is set to 0, which will accumulate the sum of the series terms.
  1. Loop to Calculate the Series:
   while k <= n:
       euler += (s.zeta(k) - 1)/k
       k += 1

This loop iterates from k=2 to k=n. In each iteration, it calculates the k-th term of the series:

  • s.zeta(k) computes the Zeta function for the given k.
  • (s.zeta(k) - 1)/k subtracts 1 from the Zeta function and divides it by k.
  • This computed value is added to the accumulating euler variable.
  1. Final Calculation:
   return 1 - euler

After the loop finishes, the function calculates Euler's constant by subtracting the accumulated euler value from 1. The result is then returned.

  1. Example:
print(eulers_constant(47))

This line demonstrates the usage of the function by printing the approximation of Euler's constant for n=47. When executed, it will output the calculated value, which should be close to the actual value of Euler's constant, approximately 0.5772.

In summary, this code uses a loop to calculate a series of terms based on the Zeta function and accumulates them to approximate Euler's constant. By providing a value for n, the code can calculate the approximation of Euler's constant to a desired level of accuracy.

Source code in the python programming language

# /**************************************************
# Subject: Computation of Euler's constant 0.5772...
#          with Euler's Zeta Series.
# tested : Python 3.11 
# -------------------------------------------------*/

from scipy import special as s

def eulers_constant(n):
    k = 2
    euler = 0
    while k <= n:
        euler += (s.zeta(k) - 1)/k
        k += 1
    return 1 - euler

print(eulers_constant(47))


  

You may also check:How to resolve the algorithm Pascal's triangle step by step in the Oforth programming language
You may also check:How to resolve the algorithm Generate random chess position step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Empty program step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Empty program step by step in the Action! programming language
You may also check:How to resolve the algorithm Special variables step by step in the ML/I programming language