Calculates array of Bernoulli numbers using recurrent relations.

View versions (2)

Interface

#include <codecogs/maths/discrete/number_theory/bernoulli_a.h>

using namespace Maths::Discrete::Number_Theory;

Given starting values with indices 0 and 1, this function calculates all numbers with indices up to some maximal index. The relations used are as follows:

$$B_0=1$$
(1)
$$B_1=-\frac{1}{2}$$
(2)
$$\frac{2n+1}{2n} B_{2n} = B_1 B_{2n-1} - \sum_{r=0}^{n-2} \frac{1}{2r+2} \big ( \begin{array}{l} 2n-1 \\ 2r+1 \end{array} \big ) B_{2r+2} B_{2n-2r-2}$$
(3)
$$B_{2n+1}=0, n=1, 2,...$$
(4)
$$B_{2n}= -\frac{2n}{2n+1} \sum_{r=0}^{n-2} \frac{1}{2r+2} \left ( \begin{array}{c} 2n-1 \\ 2r+1 \end{array} \right) B_{2r+2} B_{2n-2r-2}, n=2, 3,...$$
(5)

First numbers are:

$$\begin{array}{l} B_0 \ = \ 1 \\ B_1 \ = \ -\frac{1}{2} \\ B_2 \ = \ \frac{1}{6} \\ B_4 \ = \ -\frac{1}{30} \\ B_6 \ = \ \frac{1}{42} \\ B_8 \ = \ -\frac{1}{30} \\ B_{10} \ = \ \frac{5}{66} \\ B_{12} \ = \ -\frac{691}{2,730} \\ B_{14} \ = \ \frac{7}{6} \\ B_{16} \ = \ -\frac{3,617}{510} \\ B_{18} \ = \ \frac{43,867}{798} \\ B_{20} \ = \ -\frac{174,611}{330} \\ B_{22} \ = \ \frac{854,513}{138} \end{array}$$
(6)

References

  • Higher Transcendental Functions, vol.1, (1.13) by H.Bateman and A.Erdelyi (Bateman Manuscript Project), 1953
  • M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions, 1964 chapt.23
  • Yu.Luke, Mathematical functions and their approximations, 1975 chapt.14.2

Example 1

#include <stdio.h>
#include <codecogs/maths/discrete/number_theory/bernoulli_a.h>

#define MAX_INDEX 16

int main()
{
  double dBernoulli[MAX_INDEX+1];

  printf( "%8s%2c%20s\n", " ", 'n', "Bn" );
  printf( "%8s", " " );
  for(int i = 0; i < 22; i++ )
    printf( "%c", '-' );
  printf( "\n" );

  Maths::Discrete::Number_Theory::bernoulli_A( MAX_INDEX, dBernoulli );

  printf( "%10d%20.12f\n", 0, dBernoulli[0] );
  printf( "%10d%20.12f\n", 1, dBernoulli[1] );
  for(int i = 2; i <= MAX_INDEX; i += 2 )
    printf( "%10d%20.12f\n", i, dBernoulli[i] );
  return 0;
}

Output:

n                  Bn
        ----------------------
         0      1.000000000000
         1     -0.500000000000
         2      0.166666666667
         4     -0.033333333333
         6      0.023809523810
         8     -0.033333333333
        10      0.075757575758
        12     -0.253113553114
        14      1.166666666667
        16     -7.092156862745

Parameters

iMax
input maximal index requested
dB
output pointer to the array of numbers declared in the calling module. Array dimension should be iMax+1 or greater
GPL Licence — free for non commercial use. See Licence details.