Python is one of the most loved and most popular programming languages today. It is very user-friendly, easy to use, and has a very wide range of applications. Using Python, you can quickly write programs to accomplish tasks and goals. One of the programs you can write using Python is to print the Pascal Triangle.
How to Print a Pascal Triangle in Python?
Printing a Pascal Triangle in Python, There are different ways and programs that you can write to print a Pascal Triangle in Python. Let us explore these methods. Using the nCr Pascal Triangle Formula.
Here is what the implementation of this algorithm will look like.
# input n n = 6 for i in range(n): for j in range(n-1+1): # for left spacing print(end=” “) for j in range(i+1); # nCr = n!/((n-r)!*r!) print(factorial(i)//(factorial(j)*factorial(i-j)), end=” “) # for new line print()
What is a Pascal Triangle? & It’s Formula
A Pascal’s Triangle is a triangular arrangement of numbers that gives you the coefficients for expanding any binomial expression. The most common applications of Pascal’s triangle include probability and algebra. So, if you were looking to find the probability of getting heads or tails when tossing a coin, you can use a Pascal Triangle to get your answer.
The key property of a Pascal Triangle is that the sum of each of the adjacent two numbers of the previous row is the number that lies just below in the second row. The Pascal Triangle is a triangular pattern based on the nCr formula. It can be mathematically represented as shown below:
Using the nCr formula
A combination is the selection of elements from a group of elements where the order of these elements does not matter. Suppose you have six books on your bookshelf and you want to find out how many different combinations are possible to select any 3 books from the six with the order of the books not mattering; you would use the formula nCr, i.e. 6C3 ways. You can further represent this using the Pascal Triangle.
Let us figure out the algorithm for this method. The following are some points to consider:
- Define the input for the program and assign it a variable (n).
- There are two iterations in the loop – the outer one and the inner one. Let us call the outer iteration i and the inner one j.
- The outer loop runs for n number of times while the inner one runs from o to n-1.
- We use “ “ to account for blank spaces, and // is used to close the loop.
- Every new line is marked off in the program using \n
Time Complexity with the O(N2) Formula
The second method to print a Pascal Triangle in Python is to follow the binomial coefficient concept. Here, the binomial coefficient for an ith entry in a line is C(line, i).
Let us figure out the algorithm behind this method. The key principle behind this method of printing a Pascal Triangle in Python is to calculate the value of the binomial coefficient using C(line, i-1). So, the underlying algorithm for this is as shown:
C(line, i) = C(line, i-1) * (line – i + 1)/i
Based on this algorithm, the implementation of this logic will be:
# input n n = 6 for i in range(1, n+1): for j in range(0, n-i+1): print(‘ ‘, end=’ ‘) # first element is always 1 c=1 for j in range(1, i+1): # first value in a line is always 1 Print(‘ ‘, C, sep=’ ‘, end =’ ‘) # using binomial coefficient C = C * (i – j) // j print()