# Re: [Scilab-users] Bernoulli numbers calculation

```Hello Lester,
```
```
Le 29/12/2021 à 09:00, Lester Anderson a écrit :
```
```Hello,

```
A quick query. Have adapted existing Matlab code for Scilan to calculate Bernoulli numbers using an explicit formula (www.bernoulli.org <http://www.bernoulli.org>).
```
```
Not so explicit. Could you please provide the formula, before implementing it in Scilab language? The following is a "vectorized" version of your code, likely with the same mistake:
```
B=[];
for  m  =  0:20
Sum  =  0;
for  k=0:m
v  =  0:k;
Sum  =  Sum  +  sum(((-1).^v).*nchoosek(k,v).*(v.^m)/(k+1));
end
B(m+1)  =  Sum;
end

```
A working implementation based on the recurrent formula Bm=−1m+1∑k=0m−1(m+1k)Bk
```
could be:

mMax  =  20;
B  =  zeros(1,mMax);
B(1:2)  =  [1  -1/2];
for  m  =  2:2:mMax
B(1,m+1)  =  -sum(nchoosek(m+1,0:m-1).*B(1,1:m))/(m+1);
```
end--> B ans  =         column 1 to 13   1.  -0.5   0.1666667   0.  -0.0333333   0.   0.0238095   0.  -0.0333333   0.   0.0757576   0.  -0.2531136
```

column 14 to 21
0.   1.1666667   0.  -7.0921569   0.   54.971178   0. -529.12424

```
To fasten the loop, the call to nchoosek() could be replaced with the 3 last rows of Pascal's triangle.
```
Regards
Samuel
```
```_______________________________________________
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users
```