Bell number \$B(n)\$ is defined as the number of ways of splitting \$n\$ into any number of parts, also defined as the sum of previous \$n\$ Stirling numbers of second kind.
Here is a snippet of Python code that Wikipedia provides (slightly modified) to print bell numbers:
def bell_numbers(start, stop):
t = [[1]] ## Initialize the triangle as a two-dimensional array
c = 1 ## Bell numbers count
while c <= stop:
if c >= start:
yield t[-1][0] ## Yield the Bell number of the previous row
row = [t[-1][-1]] ## Initialize a new row
for b in t[-1]:
row.append(row[-1] + b) ## Populate the new row
c += 1 ## We have found another Bell number
t.append(row) ## Append the row to the triangle
for b in bell_numbers(1, 9):
print b
But I have to print the \$n\$th bell number, so what I did was I changed the second last line of code as follows:
for b in bell_numbers(n,n)
This does the job, but I was wondering of an even better way to print the \$n\$th bell number.