Bernoulli numbers: Difference between revisions

m
 
(9 intermediate revisions by 4 users not shown)
Line 1,986:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Bernoulli_numbers}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution.''' The following function reduces to the n-th Bernoulli number. It is a replica of the Akiyama–Tanigawa algorithm.
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Bernoulli numbers 01.png]]
 
'''Test case.''' Showing the Bernoulli numbers B<sub>0</sub> to B<sub>60</sub>
 
[[File:Fōrmulæ - Bernoulli numbers 02.png]]
 
[[File:Fōrmulæ - Bernoulli numbers 03.png]]
 
In '''[https://formulae.org/?example=Bernoulli_numbers this]''' page you can see the program(s) related to this task and their results.
=={{header|GAP}}==
 
Line 2,811 ⟶ 2,818:
58 84483613348880041862046775994036021/354
60 -(1215233140483755572040304994079820246041491/56786730)</pre>
 
=={{header|Maxima}}==
Using built-in function bern
<syntaxhighlight lang="maxima">
block(makelist([sconcat("B","(",i,")","="),bern(i)],i,0,60),
sublist(%%,lambda([x],x[2]#0)),
table_form(%%))
</syntaxhighlight>
{{inout}}
<pre>
matrix(
["B(0)=", 1],
["B(1)=", -1/2],
["B(2)=", 1/6],
["B(4)=", -1/30],
["B(6)=", 1/42],
["B(8)=", -1/30],
["B(10)=", 5/66],
["B(12)=", -691/2730],
["B(14)=", 7/6],
["B(16)=", -3617/510],
["B(18)=", 43867/798],
["B(20)=", -174611/330],
["B(22)=", 854513/138],
["B(24)=", -236364091/2730],
["B(26)=", 8553103/6],
["B(28)=", -23749461029/870],
["B(30)=", 8615841276005/14322],
["B(32)=", -7709321041217/510],
["B(34)=", 2577687858367/6],
["B(36)=", -26315271553053477373/1919190],
["B(38)=", 2929993913841559/6],
["B(40)=", -261082718496449122051/13530],
["B(42)=", 1520097643918070802691/1806],
["B(44)=", -27833269579301024235023/690],
["B(46)=", 596451111593912163277961/282],
["B(48)=", -5609403368997817686249127547/46410],
["B(50)=", 495057205241079648212477525/66],
["B(52)=", -801165718135489957347924991853/1590],
["B(54)=", 29149963634884862421418123812691/798],
["B(56)=", -2479392929313226753685415739663229/870],
["B(58)=", 84483613348880041862046775994036021/354],
["B(60)=", -1215233140483755572040304994079820246041491/56786730]
)
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import bignum
Line 3,925 ⟶ 3,978:
|
≪ DUP2 IM * ROT IM ROT RE * -
≫ ''''<span style="color:blue">FracSub'''</span>' STO
≪ DUP C→R ABS SWAP ABS DUP2 < ≪ SWAP ≫ IFT
'''WHILE''' DUP '''REPEAT''' SWAP OVER MOD '''END'''
DROP /
≫ ''''<span style="color:blue">FracSimp'''</span>' STO
Line 3,936 ⟶ 3,989:
1 m R→C +
'''IF''' m 2 ≥ '''THEN''' m 2 '''FOR''' j
DUP j 1 - GET OVER j GET '''<span style="color:blue">FracSub'''</span>
C→R SWAP j 1 - * SWAP R→C
'''<span style="color:blue">FracSimp'''</span> j 1 - SWAP PUT
-1 '''STEP END'''
'''NEXT''' 1 GET DUP RE SWAP 0 '''IFTE'''
≫ ''''<span style="color:blue">BRNOU'''</span>' STO
|
''( (a,b) (c,d) -- (e,f) )'' with e/f = a/b - c/d
Line 3,962 ⟶ 4,015:
|}
5 <span style="color:blue">BRNOU</span>
{{in}}
22 <span style="color:blue">BRNOU</span>
<pre>
5 BRNOU
22 BRNOU
</pre>
{{out}}
<pre>
Line 3,972 ⟶ 4,022:
1: (854513,138)
</pre>
 
===HP-49+ version===
Latest RPL implementations can natively handle long fractions and generate Bernoulli numbers.
{{works with|HP|49}}
≪ { }
0 ROT '''FOR''' n
'''IF''' n 2 > LASTARG MOD AND NOT '''THEN''' n IBERNOULLI + '''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
 
60 <span style="color:blue">TASK</span>
{{out}}
<pre>
1: {1 -1/2 1/6 -1/30 1/42 -1/30 5/66 -691/2730 7/6 -3617/510 43867/798 -174611/330 854513/138 -236364091/2730 8553103/6 -23749461029/870 8615841276005/14322 -7709321041217/510 2577687858367/6 -26315271553053477373/1919190 2929993913841559/6 -261082718496449122051/13530 1520097643918070802691/1806 -27833269579301024235023/690 596451111593912163277961/282 -5609403368997817686249127547/46410 495057205241079648212477525/66 -801165718135489957347924991853/1590 9149963634884862421418123812691/798 -2479392929313226753685415739663229/870 84483613348880041862046775994036021/354 -1215233140483755572040304994079820246041491/56786730}
</pre>
Runs in 3 minutes 40 on a HP-50g, against 1 hour and 30 minutes if calculating Bernoulli numbers with the above function.
 
=={{header|Ruby}}==
Line 4,895 ⟶ 4,961:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigRat
 
var bernoulli = Fn.new { |n|
Line 4,952 ⟶ 5,018:
B(60) = -1215233140483755572040304994079820246041491 / 56786730
</pre>
 
=={{header|zkl}}==
{{trans|EchoLisp}}
2,120

edits