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:
The first 15 Bell numbers: 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597, 27644437, 190899322
The fiftieth Bell number : 10726137154573358400342215518590002633917247281