[Scilab-users] Bernoulli numbers calculation

Lester Anderson arctica1963 at gmail.com
Wed Dec 29 09:00:05 CET 2021


Hello,

A quick query. Have adapted existing Matlab code for Scilan to calculate
Bernoulli numbers using an explicit formula (www.bernoulli.org). The code
works fine for numbers from 0 to 10, but 11 onwards the values are
incorrect:

Not the most efficient code but do need to understand where the errors are
coming in.

clear

function Binomial_coefficient=bcoeff(n,k)
    Binomial_coefficient=factorial(n)/(factorial(k)*factorial(n-k))
endfunction

B=[];
for m=0:20
    sum2=0;
    for k=0:m
        sum1=0;
            for v=0:k
             sum1=sum1+ (((-1)^v)*bcoeff(k,v)*(v^m)/(k+1));
            end
        sum2=sum2+sum1;
    end
B(m+1)=sum2;
end

Output of B(n) for 0 to 20 (left column), right column = expected values;
note B(7) and B(9)  show rounding issues*

   1.
  -0.5
   0.1666667
   0.
  -0.0333333
   0.
   0.0238095
   7.503D-12*  0.0
  -0.0333333
   2.215D-08*  0.0
   0.0757573
   0.0000024  0.0
  -0.2520505  −0.253113553
  -0.0072098  0.0
   1.2467346  +1.166666666
  -12.6875    0.0
  -827.875    −7.092156862
   12752.25   0.0
  -2091400.   +54.97117794
   2.104D+09  0.0
   6.417D+10  −529.1242424

Also, is it possible to print/write values in a more readable form, e.g.:
B(0) = 1
B(1) =  -0.5
...etc
B(20) = −529.1242424

Thanks for any pointers
Lester
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20211229/90cba54c/attachment.htm>


More information about the users mailing list