Bell numbers
Built-in:
say 15.of { .bell }Formula as a sum of Stirling numbers of the second kind:
func bell(n) { sum(0..n, {|k| stirling2(n, k) }) }Via Aitken's array (optimized for space):
func bell_numbers (n) {
var acc = []
var bell = [1]
(n-1).times {
acc.unshift(bell[-1])
acc.accumulate!
bell.push(acc[-1])
}
bell
}
var B = bell_numbers(50)
say "The first 15 Bell numbers: #{B.first(15).join(', ')}"
say "The fiftieth Bell number : #{B[50-1]}"Output:
Aitken's array:
Output:
Aitken's array (recursive definition):
(same output as above)
Last updated
Was this helpful?