Bernoulli numbers: Difference between revisions

(Added Wren)
Line 2,402:
58 84483613348880041862046775994036021/354
60 -(1215233140483755572040304994079820246041491/56786730)</pre>
 
=={{header:Nim}}==
<lang Nim>import bignum
import strformat
 
const Lim = 60
 
#---------------------------------------------------------------------------------------------------
 
proc bernouilli(n: Natural): Rat =
## Compute a Bernouilli number using Akiyama–Tanigawa algorithm.
 
var a = newSeq[Rat](n + 1)
for m in 0..n:
a[m] = newRat(1, m + 1)
for j in countdown(m, 1):
a[j-1] = j * (a[j-1] - a[j])
result = a[0]
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
type Info = tuple
n: int # Number index in Bernouilli sequence.
val: Rat # Bernouilli number.
 
var values: seq[Info] # List of values as Info tuples.
var maxLen = -1 # Maximum length.
 
# First step: compute the values and prepare for display.
for n in 0..Lim:
# Compute value.
if n != 1 and (n and 1) == 1: continue # Ignore odd "n" except 1.
let b = bernouilli(n)
# Check numerator length.
let len = ($b.num).len
if len > maxLen: maxLen = len
# Store information for next step.
values.add((n, b))
 
# Second step: display the values with '/' aligned.
for (n, b) in values:
let s = fmt"{($b.num).alignString(maxLen, '>')} / {b.denom}"
echo fmt"{n:2}: {s}"</lang>
 
{{out}}
<pre> 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</pre>
 
=={{header|PARI/GP}}==
Anonymous user