Euler's constant 0.5772...

From Rosetta Code
Revision as of 16:41, 22 November 2021 by rosettacode>Udo e. pops (Computing Euler's constant 0.5772...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Euler's constant 0.5772...
You are encouraged to solve this task according to the task description, using any language you may know.


Task.

Compute Euler's 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).

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.


References.

[1] Gourdon and Sebah, The Euler constant γ.   (for all formula's)

[2] Euler's original journal article translated from the latin (p. 9)



C

Single precision

<lang c>/********************************************* Subject: Comparing five methods for

        computing Euler's constant 0.5772...

tested : tcc-0.9.27


*/

  1. include <math.h>
  2. include <stdio.h>
  1. define eps 1e-6

int main(void) { double a, b, h, n2, r, u, v; int k, k2, m, n;

printf("From the definition, err. 3e-10\n");

n = 400;

h = 1; for (k = 2; k <= n; k++) {

  h += 1.0 / k;

} //faster convergence: Negoi, 1999 a = log(n +.5 + 1.0 / (24*n));

printf("Hn  %.16f\n", h); printf("gamma %.16f\nk = %d\n\n", h - a, n);


printf("Sweeney, 1963, err. idem\n");

n = 21;

double s[] = {0, n}; r = n; k = 1; do {

  k += 1;
  r *= (double) n / k;
  s[k & 1] += r / k;

} while (r > eps);

printf("gamma %.16f\nk = %d\n\n", s[1] - s[0] - log(n), k);


printf("Bailey, 1988\n");

n = 5;

a = 1; h = 1; n2 = pow(2,n); r = 1; k = 1; do {

  k += 1;
  r *= n2 / k;
  h += 1.0 / k;
  b = a; a += r * h;

} while (fabs(b - a) > eps); a *= n2 / exp(n2);

printf("gamma %.16f\nk = %d\n\n", a - n * log(2), k);


printf("Brent-McMillan, 1980\n");

n = 13;

a = -log(n); b = 1; u = a; v = b; n2 = n * n; k2 = 0; k = 0; do {

  k2 += 2*k + 1;
  k += 1;
  a *= n2 / k;
  b *= n2 / k2;
  a = (a + b) / k;
  u += a;
  v += b;

} while (fabs(a) > eps);

printf("gamma %.16f\nk = %d\n\n", u / v, k);


printf("How Euler did it in 1735\n"); //Bernoulli numbers with even indices double B2[] = {1.0,1.0/6,-1.0/30,1.0/42,-1.0/30,\

5.0/66,-691.0/2730,7.0/6,-3617.0/510,43867.0/798};

m = 7; if (m > 9) return(0);

n = 10;

//n-th harmonic number h = 1; for (k = 2; k <= n; k++) {

  h += 1.0 / k;

} printf("Hn  %.16f\n", h);

h -= log(n); printf(" -ln %.16f\n", h);

//expansion C = -digamma(1) a = -1.0 / (2*n); n2 = n * n; r = 1; for (k = 1; k <= m; k++) {

  r *= n2;
  a += B2[k] / (2*k * r);

}

printf("err  %.16f\ngamma %.16f\nk = %d", a, h + a, n + m);

printf("\n\nC = 0.57721566490153286...\n"); }</lang>

output:
From the definition, err. 3e-10
Hn    6.5699296911765055
gamma 0.5772156645765731
k = 400

Sweeney, 1963, err. idem
gamma 0.5772156645636311
k = 68

Bailey, 1988
gamma 0.5772156649015341
k = 89

Brent-McMillan, 1980
gamma 0.5772156649015329
k = 40

How Euler did it in 1735
Hn    2.9289682539682538
  -ln 0.6263831609742079
err  -0.0491674960726754
gamma 0.5772156649015325
k = 17

C  =  0.57721566490153286...

Multi precision

From first principles

<lang c>/************************************************** Subject: Computation of Euler's constant 0.5772...

        with the Brent-McMillan algorithm B1,
        Math. Comp. 34 (1980), 305-312

tested : tcc-0.9.27 with gmp 6.2.0


*/

  1. include <gmp.h>
  2. include <stdio.h>
  3. include <stdlib.h>
  4. include <time.h>

//multi-precision float pointers mpf_ptr u, v, k2;

//precision parameters unsigned long e10, e2; long e; double f;

//log(x/y) with the Taylor series for atanh(x-y/x+y) void ln (mpf_ptr s, unsigned long x, unsigned long y) { mpf_ptr d = u, q = v; unsigned long k;

  //Möbius transformation
  k = x; x -= y; y += k;
  if (x != 1) {
     printf ("ln: illegal argument x - y != 1");
     exit;
  }
  //s = 1 / (x + y)
  mpf_set_ui (s, y);
  mpf_ui_div (s, 1, s);
  //k2 = s * s
  mpf_mul (k2, s, s);
  mpf_set (d, s);
  k = 1;
  do {
     k += 2;
     //d *= k2
     mpf_mul (d, d, k2);
     //q = d / k
     mpf_div_ui (q, d, k);
     //s += q
     mpf_add (s, s, q);
     f = mpf_get_d_2exp (&e, q);
  } while (abs(e) < e2);
  //s *= 2
  mpf_mul_2exp (s, s, 1);

}

int main (void) { mpf_ptr a = malloc(sizeof(__mpf_struct)); mpf_ptr b = malloc(sizeof(__mpf_struct)); u = malloc(sizeof(__mpf_struct)); v = malloc(sizeof(__mpf_struct)); k2 = malloc(sizeof(__mpf_struct)); //unsigned long integers unsigned long k, n, n2, r, s, t;

clock_t tim = clock();

// n = 2^i * 3^j * 5^k

// log(n) = r * log(16/15) + s * log(25/24) + t * log(81/80)

// solve linear system for r, s, t // 4 -3 -4| i // -1 -1 4| j // -1 2 -1| k

//examples t = 1; switch (t) { case 1 :

  n = 60;
  r = 41;
  s = 30;
  t = 18;

//100 digits break; case 2 :

  n = 4800;
  r = 85;
  s = 62;
  t = 37;

//8000 digits, 0.6 s break; case 3 :

  n = 9375;
  r = 91;
  s = 68;
  t = 40;

//15625 digits, 2.5 s break; default :

  n = 18750;
  r = 98;
  s = 73;
  t = 43;

//31250 digits, 12 s. @2.00GHz }

//decimal precision e10 = n / .6; //binary precision e2 = (1 + e10) / .30103;

//initialize mpf's mpf_set_default_prec (e2); mpf_inits (a, b, u, v, k2, (mpf_ptr)0);

//Compute log terms

ln(b, 16, 15);

//a = r * b mpf_mul_ui (a, b, r);

ln(b, 25, 24);

//a += s * b mpf_mul_ui (u, b, s); mpf_add (a, a, u);

ln(b, 81, 80);

//a += t * b mpf_mul_ui (u, b, t); mpf_add (a, a, u);

//gmp_printf ("log(%lu) %.*Ff\n", n, e10, a);

//B&M, algorithm B1

//a = -a, b = 1 mpf_neg (a, a); mpf_set_ui (b, 1); mpf_set (u, a); mpf_set (v, b);

k = 0; n2 = n * n; //k2 = k * k mpf_set_ui (k2, 0); do {

  //k2 += 2k + 1
  mpf_add_ui (k2, k2, (k << 1) + 1);
  k += 1;
  //b = b * n2 / k2
  mpf_div (b, b, k2);
  mpf_mul_ui (b, b, n2);
  //a = (a * n2 / k + b) / k
  mpf_div_ui (a, a, k);
  mpf_mul_ui (a, a, n2);
  mpf_add (a, a, b);
  mpf_div_ui (a, a, k);
  //u += a, v += b
  mpf_add (u, u, a);
  mpf_add (v, v, b);
  f = mpf_get_d_2exp (&e, a);

} while (abs(e) < e2);

mpf_div (u, u, v); gmp_printf ("gamma %.*Ff (maxerr. 1e-%lu)\n", e10, u, e10);

gmp_printf ("k = %lu\n\n", k);

tim = clock() - tim; printf("time: %.7f s\n",((double)tim)/CLOCKS_PER_SEC); }</lang>

output:
gamma 0.5772156649015328606065120900824024310421593359399235988057672348848677267776646709369470632917467495 (maxerr. 1e-100)
k = 255

The easy way

<lang c>/******************************************* Subject: Euler's constant 0.5772... tested : tcc-0.9.27 with mpfr 4.1.0


*/

  1. include <gmp.h>
  2. include <mpfr.h>
  3. include <stdio.h>
  4. include <stdlib.h>
  5. include <time.h>

int main (void) { mpfr_ptr a = malloc(sizeof(__mpfr_struct)); unsigned long e2, e10; clock_t tim = clock();

//decimal precision e10 = 100;

//binary precision e2 = (1 + e10) / .30103; mpfr_init2 (a, e2);

mpfr_const_euler (a, MPFR_RNDN); mpfr_printf ("gamma %.*Rf\n\n", e10, a);

tim = clock() - tim; gmp_printf ("time: %.7f s\n",((double)tim)/CLOCKS_PER_SEC); }</lang>


FreeBASIC

Single precision

<lang freebasic>'********************************************** 'Subject: Comparing five methods for ' computing Euler's constant 0.5772... 'tested : FreeBasic 1.08.1 '---------------------------------------------- const eps = 1e-6 dim as double a, b, h, n2, r, u, v dim as integer k, k2, m, n

? "From the definition, err. 3e-10"

n = 400

h = 1 for k = 2 to n

  h += 1 / k

next k 'faster convergence: Negoi, 1999 a = log(n +.5 + 1 / (24*n))

? "Hn "; h ? "gamma"; h - a; !"\nk ="; n ?


? "Sweeney, 1963, err. idem"

n = 21

dim as double s(1) = {0, n} r = n k = 1 do

  k += 1
  r *= n / k
  s(k and 1) += r / k

loop until r < eps

? "gamma"; s(1) - s(0) - log(n); !"\nk ="; k ?


? "Bailey, 1988"

n = 5

a = 1 h = 1 n2 = 2^n r = 1 k = 1 do

  k += 1
  r *= n2 / k
  h += 1 / k
  b = a: a += r * h

loop until abs(b - a) < eps a *= n2 / exp(n2)

? "gamma"; a - n * log(2); !"\nk ="; k ?


? "Brent-McMillan, 1980"

n = 13

a = -log(n) b = 1 u = a v = b n2 = n * n k2 = 0 k = 0 do

  k2 += 2*k + 1
  k += 1
  a *= n2 / k
  b *= n2 / k2
  a = (a + b) / k
  u += a
  v += b

loop until abs(a) < eps

? "gamma"; u / v; !"\nk ="; k ?


? "How Euler did it in 1735" 'Bernoulli numbers with even indices dim as double B2(9) = {1,1/6,-1/30,1/42,_

-1/30,5/66,-691/2730,7/6,-3617/510,43867/798}

m = 7 if m > 9 then end

n = 10

'n-th harmonic number h = 1 for k = 2 to n

  h += 1 / k

next k ? "Hn "; h

h -= log(n) ? " -ln"; h

'expansion C = -digamma(1) a = -1 / (2*n) n2 = n * n r = 1 for k = 1 to m

  r *= n2
  a += B2(k) / (2*k * r)

next k

? "err "; a; !"\ngamma"; h + a; !"\nk ="; n + m ? ? "C = 0.57721566490153286..." end</lang>

output:
From the definition, err. 3e-10
Hn    6.569929691176506
gamma 0.5772156645765731
k = 400

Sweeney, 1963, err. idem
gamma 0.5772156645636311
k = 68

Bailey, 1988
gamma 0.5772156649015341
k = 89

Brent-McMillan, 1980
gamma 0.5772156649015329
k = 40

How Euler did it in 1735
Hn    2.928968253968254
  -ln 0.6263831609742079
err  -0.04916749607267539
gamma 0.5772156649015325
k = 17

C  =  0.57721566490153286...

Multi precision

From first principles <lang freebasic>'*************************************************** 'Subject: Computation of Euler's constant 0.5772... ' with the Brent-McMillan algorithm B1, ' Math. Comp. 34 (1980), 305-312 'tested : FreeBasic 1.08.1 with gmp 6.2.0 '---------------------------------------------------

  1. include "gmp.bi"

'multi-precision float pointers Dim as mpf_ptr a, b Dim shared as mpf_ptr k2, u, v 'unsigned long integers Dim as ulong k, n, n2, r, s, t 'precision parameters Dim shared as ulong e10, e2 Dim shared e as clong Dim shared f as double Dim as double tim = TIMER CLS

a = allocate(len(__mpf_struct)) b = allocate(len(__mpf_struct)) u = allocate(len(__mpf_struct)) v = allocate(len(__mpf_struct)) k2 = allocate(len(__mpf_struct))

'log(x/y) with the Taylor series for atanh(x-y/x+y) Sub ln (byval s as mpf_ptr, byval x as ulong, byval y as ulong) Dim as mpf_ptr d = u, q = v Dim k as ulong

  'Möbius transformation
  k = x: x -= y: y += k
  If x <> 1 Then
     Print "ln: illegal argument x - y <> 1"
     End
  End If
  's = 1 / (x + y)
  mpf_set_ui (s, y)
  mpf_ui_div (s, 1, s)
  'k2 = s * s
  mpf_mul (k2, s, s)
  mpf_set (d, s)
  k = 1
  Do
     k += 2
     'd *= k2
     mpf_mul (d, d, k2)
     'q = d / k
     mpf_div_ui (q, d, k)
     's += q
     mpf_add (s, s, q)
     f = mpf_get_d_2exp (@e, q)
  Loop until abs(e) > e2
  's *= 2
  mpf_mul_2exp (s, s, 1)

End Sub

'Main

'n = 2^i * 3^j * 5^k

'log(n) = r * log(16/15) + s * log(25/24) + t * log(81/80)

'solve linear system for r, s, t ' 4 -3 -4| i '-1 -1 4| j '-1 2 -1| k

'examples t = 1 select case t case 1

  n = 60
  r = 41
  s = 30
  t = 18

'100 digits case 2

  n = 4800
  r = 85
  s = 62
  t = 37

'8000 digits, 0.6 s case 3

  n = 9375
  r = 91
  s = 68
  t = 40

'15625 digits, 2.5 s case else

  n = 18750
  r = 98
  s = 73
  t = 43

'31250 digits, 12 s. @2.00GHz end select

'decimal precision e10 = n / .6 'binary precision e2 = (1 + e10) / .30103

'initialize mpf's mpf_set_default_prec (e2) mpf_inits (a, b, u, v, k2, Cptr(mpf_ptr, 0))

'Compute log terms

ln b, 16, 15

'a = r * b mpf_mul_ui (a, b, r)

ln b, 25, 24

'a += s * b mpf_mul_ui (u, b, s) mpf_add (a, a, u)

ln b, 81, 80

'a += t * b mpf_mul_ui (u, b, t) mpf_add (a, a, u)

gmp_printf (!"log(%lu) %.*Ff\n", n, e10, a)

'B&M, algorithm B1

'a = -a, b = 1 mpf_neg (a, a) mpf_set_ui (b, 1) mpf_set (u, a) mpf_set (v, b)

k = 0 n2 = n * n 'k2 = k * k mpf_set_ui (k2, 0) do

  'k2 += 2k + 1
  mpf_add_ui (k2, k2, (k shl 1) + 1)
  k += 1
  'b = b * n2 / k2
  mpf_div (b, b, k2)
  mpf_mul_ui (b, b, n2)
  'a = (a * n2 / k + b) / k
  mpf_div_ui (a, a, k)
  mpf_mul_ui (a, a, n2)
  mpf_add (a, a, b)
  mpf_div_ui (a, a, k)
  'u += a, v += b
  mpf_add (u, u, a)
  mpf_add (v, v, b)
  f = mpf_get_d_2exp (@e, a)

Loop until abs(e) > e2

mpf_div (u, u, v) gmp_printf (!"gamma %.*Ff (maxerr. 1e-%lu)\n", e10, u, e10)

gmp_printf (!"k = %lu\n\n", k)

gmp_printf (!"time: %.7f s\n", TIMER - tim) end</lang>

output:
gamma 0.5772156649015328606065120900824024310421593359399235988057672348848677267776646709369470632917467495 (maxerr. 1e-100)
k = 255

The easy way

<lang freebasic>' ****************************************** 'Subject: Euler's constant 0.5772... 'tested : FreeBasic 1.08.1 with mpfr 4.1.0 '-------------------------------------------

  1. include "gmp.bi"
  2. include "mpfr.bi"

dim as mpfr_ptr a = allocate(len(__mpfr_struct)) dim as ulong e2, e10 dim as double tim = TIMER

'decimal precision e10 = 100

'binary precision e2 = (1 + e10) / .30103 mpfr_init2 (a, e2)

mpfr_const_euler (a, MPFR_RNDN) mpfr_printf (!"gamma %.*Rf\n\n", e10, a)

gmp_printf (!"time: %.7f s\n", TIMER - tim) end</lang>


PARI/GP

built-in: <lang parigp>\l "euler_const.log" \p 100 print("gamma ", Euler); \q</lang>