Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,28 @@
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

View file

@ -0,0 +1,51 @@
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 = move(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 the 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 = move(newRow)
yield row
when isMainModule:
import strformat
import strutils
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
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