Skip to content

Commit 579975b

Browse files
authored
Merge pull request CoderDojoSV#16 from amanv1999/master
python program to print bell number
2 parents 0fa8d4b + 83d6275 commit 579975b

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎bell_number.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Python program to print bell number
2+
#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)
3+
A sample Bell triangle is as follows:
4+
1
5+
1 3
6+
3 8 13
7+
13 23 33 43
8+
#The code to print the bell triangle is as follows-
9+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
10+
n=int(input("enter the number of bell")) #taking value from the user
11+
bell=0 #initialising bell to 'zero'
12+
k=0 #initialising k to 'zero'
13+
for i in range(0,n): #loop for changing rows from 0 to n
14+
for j in range(0,i+1): #printing columns
15+
if j==0 and i>0: #repeating the last number of previous row in new row
16+
print(bell,'',end='') #printing first number of each line
17+
else:
18+
k=(i**2)+1+bell #to generate other numbers of line
19+
print(k,'',end='') #printing other number in lines
20+
bell=k #updating value of bell
21+
print('\n') #for moving into next lines
22+
print("last number of bell is",bell)

0 commit comments

Comments
 (0)