Rebol3 Code Examplex
Bell numbers
Enumerations of the number of different ways to partition a set that has exactly n elements.
Rebol [
title: "Rosetta code: Bell numbers"
file: %Bell_numbers.r3
url: https://rosettacode.org/wiki/Bell_numbers
]
bell-triangle: function [
"Compute Bell triangle with n rows (1-based; row 0 unused)."
n [integer!]
][
tri: make block! n
repeat i n [append/only tri array/initial i 0] ;; row i has i zero-filled elements
tri/2/1: 1 ;; seed: first element of row 2
for i 3 n 1 [
tri/:i/1: tri/(i - 1)/(i - 2) ;; first element = second-to-last of previous row
for j 2 i 1 [
tri/:i/:j: tri/:i/(j - 1) + tri/(i - 1)/(j - 1) ;; sum of left and upper-left
]
]
tri
]
bt: bell-triangle 16
print "First fifteen Bell numbers:"
for i 1 15 1 [
printf [-2 ": "] [i bt/(i + 1)/1]
]
print "^/The first ten rows of Bell's triangle:"
for i 1 10 1 [
print bt/(i + 1)
]Output:
First fifteen Bell numbers:
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
The first ten rows of Bell's triangle:
1 0
1 2 2
2 3 5 7
5 7 10 15 22
15 20 27 37 52 74
52 67 87 114 151 203 277
203 255 322 409 523 674 877 1154
877 1080 1335 1657 2066 2589 3263 4140 5294
4140 5017 6097 7432 9089 11155 13744 17007 21147 26441
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975 142416