Bell numbers: Difference between revisions

(Added AutoHotkey)
Line 1,430:
36401, 43833, 52922, 64077, 77821, 94828, 115975}}
</lang>
 
=={{header|Nim}}==
==={{Using Recurrence relation}}==
<lang Nim>import math
 
iterator b(): int =
## Iterator yielding the bell numbers.
var numbers = @[1]
yield 1
var n = 0
while true:
var next = 0
for k in 0..n:
next += binom(n, k) * numbers[k]
numbers.add(next)
yield next
inc n
 
when isMainModule:
 
import strformat
 
const Limit = 25 # Maximum index beyond which an overflow occurs.
 
echo "Bell numbers from B0 to B25:"
var i = 0
for n in b():
echo fmt"{i:2d}: {n:>20d}"
inc i
if i > Limit:
break</lang>ell numbers from B0 to B25:
0: 1
1: 1
2: 2
3: 5
4: 15
5: 52
6: 203
7: 877
8: 4140
9: 21147
10: 115975
11: 678570
12: 4213597
13: 27644437
14: 190899322
15: 1382958545
16: 10480142147
17: 82864869804
18: 682076806159
19: 5832742205057
20: 51724158235372
21: 474869816156751
22: 4506715738447323
23: 44152005855084346
24: 445958869294805289
25: 4638590332229999353</pre>
 
==={{Using Bell triangle}}==
<lang Nim>
iterator b(): int =
## Iterator yielding the bell numbers.
var row = @[1]
yield 1
yield 1
while true:
var newRow = newSeq[int](row.len + 1)
newRow[0] = row[^1]
for i in 1..newRow.high:
newRow[i] = newRow[i - 1] + row[i - 1]
row.shallowCopy(newRow)
yield row[^1] # The last value of the row is one step ahead of the first one.
 
iterator bellTriangle(): seq[int] =
## Iterator yielding the rows of tye Bell triangle.
var row = @[1]
yield row
while true:
var newRow = newSeq[int](row.len + 1)
newRow[0] = row[^1]
for i in 1..newRow.high:
newRow[i] = newRow[i - 1] + row[i - 1]
row.shallowCopy(newRow)
yield row
 
 
when isMainModule:
 
import strformat
import strutils
 
const Limit = 25 # Maximum index beyond which an overflow coours.
 
echo "Bell numbers from B0 to B25:"
var i = 0
for n in b():
echo fmt"{i:2d}: {n:>20d}"
inc i
if i > Limit:
break
 
echo "\nFirst ten rows of Bell triangle:"
i = 0
for row in bellTriangle():
inc i
var line = ""
for val in row:
line.addSep(" ", 0)
line.add(fmt"{val:6d}")
echo line
if i == 10:
break</lang>
 
{{out}}
<pre>Bell numbers from B0 to B25:
0: 1
1: 1
2: 2
3: 5
4: 15
5: 52
6: 203
7: 877
8: 4140
9: 21147
10: 115975
11: 678570
12: 4213597
13: 27644437
14: 190899322
15: 1382958545
16: 10480142147
17: 82864869804
18: 682076806159
19: 5832742205057
20: 51724158235372
21: 474869816156751
22: 4506715738447323
23: 44152005855084346
24: 445958869294805289
25: 4638590332229999353
 
First ten rows of Bell triangle:
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
52 67 87 114 151 203
203 255 322 409 523 674 877
877 1080 1335 1657 2066 2589 3263 4140
4140 5017 6097 7432 9089 11155 13744 17007 21147
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975</pre>
 
{{out}}
<pre>
 
=={{header|Pascal}}==
Anonymous user