[Scilab-users] Bernoulli numbers calculation
Lester Anderson
arctica1963 at gmail.com
Thu Dec 30 08:59:33 CET 2021
Hello Samuel,
Thanks for the solution. As pointed out it is best to show the equation
being assessed (from www.bernoulli.org). The one I looked at was the
following:
[image: Explicit_formula.PNG]
Using nchoosek in the original code gives the same issue.
Lester
On Wed, 29 Dec 2021 at 22:46, Samuel Gougeon <sgougeon at free.fr> wrote:
> 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).
>
>
> 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 at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20211230/edf9f01b/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Explicit_formula.PNG
Type: image/png
Size: 13849 bytes
Desc: not available
URL: <https://lists.scilab.org/pipermail/users/attachments/20211230/edf9f01b/attachment.png>
More information about the users
mailing list