Skip to content

Commit

Permalink
python program to print bell number
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwarriorftw authored Oct 7, 2017
1 parent 0fa8d4b commit 83d6275
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bell_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#Python program to print bell number
#Bell Number:-Let S(n, k) be total number of partitions of n elements into k sets. The value of n’th Bell Number is sum of S(n, k) for k = 1 to n. Value of S(n, k) can be defined recursively as, S(n+1, k) = k*S(n, k) + S(n, k-1)
A sample Bell triangle is as follows:
1
1 3
3 8 13
13 23 33 43
#The code to print the bell triangle is as follows-
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
n=int(input("enter the number of bell")) #taking value from the user
bell=0 #initialising bell to 'zero'
k=0 #initialising k to 'zero'
for i in range(0,n): #loop for changing rows from 0 to n
for j in range(0,i+1): #printing columns
if j==0 and i>0: #repeating the last number of previous row in new row
print(bell,'',end='') #printing first number of each line
else:
k=(i**2)+1+bell #to generate other numbers of line
print(k,'',end='') #printing other number in lines
bell=k #updating value of bell
print('\n') #for moving into next lines
print("last number of bell is",bell)

0 comments on commit 83d6275

Please sign in to comment.