Pascal’s Triangle!

Rudransh
3 min readFeb 9, 2022

Pascal’s triangle is an arrangement of binomial coefficients in triangular form. It is named after the French mathematician Blaise Pascal. Moreover the triangle can be filled out from the top by adding together the two numbers just above to the left and right of each position in the triangle. Now I am going to explain this concept with the help of an figure.

This is binomial coefficients arrangement(fig 1)
Another way to get value !

There are several series in this triangle, some of the series are given below:

  • Fibonacci series
  • Counting numbers
  • Triangular number
  • Tetrahedral numbers

Pascal’s triangle is used widely:

  • Probability theory.
  • Combinations.
  • Algebra.

Here is the code for Pascal’s Triangle in Python:

def generate(numRows):
l = list()
for i in range(numRows):
temp = list()
for j in range(i + 1):
if j == 0 or j == i:
temp.append(1)
else:
temp.append(l[i - 1][j - 1] + l[i - 1][j])
l.append(temp)
return l
def pat(numRows, store): #function to print pyramid pattern
for i in range(numRows):
for j in range(numRows - i - 1):
print(" ", end = "")
for j in range(i + 1):
print(store[i][j], end = " ")
print()
numRows = 5
store = (generate(numRows))
pat(numRows, store)
Output of program

If user ask you to give a particular row so you can not calculate the all previous row to get that particular row. In this situation you simply use binomial concept as in fig 1.

So the code for calculating the particular row enter by the user is given below in Python language:

def getRow(rowIndex):
def fact(x):
f = 1
while x != 0:
f *= x
x -= 1
return f
res = list()
for i in range(rowIndex + 1):
res.append(fact(rowIndex) // (fact(i) * fact(rowIndex - i)))
return (res)
print(getRow(5))
Output of above code

In above code you can also use inbuild function factorial present in math module. I am writing my own fact function because I think this is more efficient as compare to inbuild function.

Now if you want to Calculate the sum of an row in this triangle you can calculate in using a simply formula i.e (2 raise to power n where n) is row number.

Happy Coding!

--

--