Write a program to print Pascal's Triangle in Python
Problem : Write a program to print Pascal's Triangle with a given number of rows.
For example : The number of rows is 5, then Pascal's Triangle will be printed as follows:
Input: n = 5 Output: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Pascal's triangle is also a question frequently used by large companies when recruiting programmers. In this article, TipsMake.com will learn with you how to write a program to print Pascal's Triangle in Python.
Method 1: Use the formula nCr ie n!/(nr)!r!
After using the nCr formula, the graphical representation becomes:
0C0 1C0 1C1 2C0 2C1 2C2 3C0 3C1 3C2 3C3
Algorithm:
- Get the number of rows to be printed, let's say it is n.
- Do the outer iteration i from 0 to n times to print the rows.
- Do the inner iteration for j from 0 to (N-1).
- Print a space "".
- Close inner loop (j loop) //required for left spacing.
- Do the inner iteration for j from 0 to i.
- Print nCr of i and j.
- Close the inner loop.
- Print the newline character (n) after each inner iteration.
Here is sample code:
# Print Pascal's Triangle in Python from math import factorial # input n n = 5 for i in range(n): for j in range(n-i+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()
Method 2:
We can optimize the above code using the following binomial coefficient concept, the i-th item in a line is the binomial coefficient C(line, i) and all lines start with the value 1. Here , we have to implement the idea of computing C(line, i) using C(line, i-1).
The sample code is as follows:
# Print Pascal's Triangle in Python # input n n = 5 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()
Method 3:
This is the most optimal way to print Pascal triangle. This method is based on powers of 11.
11**0 = 1 11**1 = 11 11**2 = 121 11**3 = 1331
The sample code is as follows:
# Print Pascal's Triangle in Python # input n n = 5 # iterarte upto n for i in range(n): # adjust space print(' '*(n-i), end='') # compute power of 11 print(' '.join(map(str, str(11**i))))
However, the disadvantage is that this method can only be applied up to n=5.
TipsMake.com hopes that this article will be useful to you.
You should read it
- How to set up Python to program on WSL
- What is Python? Why choose Python?
- More than 100 Python exercises have solutions (sample code)
- Python online editor
- 5 choose the best Python IDE for you
- Why should you learn Python programming language?
- Multiple choice quiz about Python - Part 3
- Bookmark 5 best Python programming learning websites
- Python data type: string, number, list, tuple, set and dictionary
- For in Python loop
- Multiple choice quiz about Python - Part 4
- Multiple choice test on Python - Part 11
Maybe you are interested
What is Digital Footprint?
Excel 2016 - Lesson 12: Formatting pages and printing spreadsheets in Excel
How to print multiple Word files at once on your computer
Instructions for managing print lists on iPhone
How to print Excel on 1 A4 page - Display full content on 1 page
Instructions on how to open when Print title in Excel is locked