Bernoulli numbers: Difference between revisions

Line 832:
</pre>
=={{header|factor}}==
One could use the "bernoulli" word from the math.extras vocabulary. Instead a method described by Brent and Harvey (2011) in "Fast computation of Bernoulli, Tangent and Secant numbers" https://arxiv.org/pdf/1108.0286.pdf is used.
 
<lang>
{{improve|factor|<br> The Bernoulli numbers are to be expressed as (reduced) fractions, <br> not as a sum of an integer and a fraction. <br><br> The Bernoulli numbers are to be indexed (so as to be able to understand which Bernoulli numbers are being displayed. <br><br>}}
:: bernoulli-numbers ( n -- )
 
n 1 + 0 <array> :> tab
{{incorrect|factor|<br> '''B<sub>0</sub>''' is not shown, nor is '''B<sub>1</sub>''' <br><br> All Bernoulli numbers that are equal to zero are to be suppressed. <br><br>}}
1 :> s!
1 1 tab set-nth
<lang factor>
2 n [a,b] [| k |
SYMBOL: j
k 1 - dup
SYMBOL: k
tab nth *
SYMBOL: n
k tab set-nth
SYMBOL: s
SYMBOL: tab
 
: bernoulli-numbers ( n -- seq )
n set
n get 1 + 0 <array> tab set
1 1 tab get set-nth
2 n get [a,b] [
k set
k get 1 - dup
tab get nth *
k get tab get set-nth
] each
2 n get [a,b] [| k |
k setn [a,b] [| j |
k get nj tab nth get [a,b] [
j setk - 2 + *
j get1 - tab get nth
j get k get - 2* + *
j get 1tab set- tab get nth
j get k get - * +
j get tab get set-nth
] each
] each
1 sn set[a,b] [| k |
k 2 * dup
1 n get [a,b] [
2 swap ^
k set
kdup get1 2- * dup
2 swap ^k tab nth
dupswap 1 -/ *
s * k get tab get set-nth
swaps /-1 * s!
] each
s get * k get tab get set-nth
s get -1 * s set
0 1 1 "%2d : %d / %d\n" printf
1 -1 2 "%2d : %d / %d\n" printf
1 n [a,b] [| k |
k 2 * k tab nth
[ numerator ] [ denominator ] bi
"%2d : %d / %d\n" printf
] each
tab get
;
 
IN: scratchpad 30 bernoulli-numbers
0 : 1 / 1
1 : -1 / 2
2 : 1 / 6
4 : -1 / 30
6 : 1 / 42
8 : -1 / 30
10 : 5 / 66
12 : -691 / 2730
14 : 7 / 6
16 : -3617 / 510
18 : 43867 / 798
20 : -174611 / 330
22 : 854513 / 138
24 : -236364091 / 2730
26 : 8553103 / 6
28 : -23749461029 / 870
30 : 8615841276005 / 14322
32 : -7709321041217 / 510
34 : 2577687858367 / 6
36 : -26315271553053477373 / 1919190
38 : 2929993913841559 / 6
40 : -261082718496449122051 / 13530
42 : 1520097643918070802691 / 1806
44 : -27833269579301024235023 / 690
46 : 596451111593912163277961 / 282
48 : -5609403368997817686249127547 / 46410
50 : 495057205241079648212477525 / 66
52 : -801165718135489957347924991853 / 1590
54 : 29149963634884862421418123812691 / 798
56 : -2479392929313226753685415739663229 / 870
58 : 84483613348880041862046775994036021 / 354
60 : -1215233140483755572040304994079820246041491 / 56786730
</lang>
This uses a method described by Brent and Harvey in https://arxiv.org/pdf/1108.0286.pdf to calculate the tangent numbers, and then from those the Bernoulli numbers.
<lang>
30 bernoulli-numbers .
{
0
1/6
-1/30
1/42
-1/30
5/66
-691/2730
1+1/6
-7-47/510
54+775/798
-529-41/330
6192+17/138
-86580-691/2730
1425517+1/6
-27298231-59/870
601580873+12899/14322
-15116315767-47/510
429614643061+1/6
-13711655205088-638653/1919190
488332318973593+1/6
-19296579341940068-2011/13530
841693047573682615+1/1806
-40338071854059455413-53/690
2115074863808199160560+41/282
-120866265222965259346027-14477/46410
7500866746076964366855720+5/66
-503877810148106891413789303-83/1590
36528776484818123335110430842+775/798
-2849876930245088222626914643291-59/870
238654274996836276446459819192192+53/354
-21399949257225333665810744765191097-22298681/56786730
}
</lang>
Alternatively one could of course use the "bernoulli" word from the math.extras vocabulary.
 
=={{header|FreeBASIC}}==