Table of Contents
This guide provides a clear overview of write a program to print out pascal's triangle in python, including Method 1: Use the Formula Ncr Ie N!/(Nr)!r!, Method 2. Use it to understand the topic, compare the available options, and make a more informed decision.
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.
Conclusion
Understanding Write a Program to Print Out Pascal's Triangle in Python makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.
FAQ
What is Write a Program to Print Out Pascal's Triangle in Python?
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.
Why is Write a Program to Print Out Pascal's Triangle in Python important?
Understanding Write a Program to Print Out Pascal's Triangle in Python helps you evaluate features, compatibility, performance, and potential limitations before you choose a product or follow a procedure.
What should you consider when using or choosing Write a Program to Print Out Pascal's Triangle in Python?
Consider your specific goal, compatibility requirements, available features, cost, security, and the practical recommendations described in this guide.
Reader Comments 0
Sign in with email or Google to join the discussion.