Understanding the Pascal's triangle generator — concept and principle
Pascal's triangle is a triangular array of whole numbers in which each number is the sum of the two numbers directly above it. This generator prints row n (the row you enter) and every row from 0 up to n. The rows give the binomial coefficients C(n, k), so the triangle is used to expand brackets such as (x + y)ⁿ, to count combinations, and to work out binomial probabilities. You can generate up to 40 rows.
How it is calculated
- Row n contains n + 1 numbers: C(n, 0), C(n, 1), …, C(n, n).
- Each entry is C(n, k) = n! ÷ (k!(n − k)!), the number of ways to choose k items from n.
- Equivalently, every row starts and ends with 1, and each inner entry is the sum of the two entries above it: C(n, k) = C(n − 1, k − 1) + C(n − 1, k).
- Rows are numbered from 0, so the single "1" at the top is row 0.
The principle
The addition rule has a counting explanation. To choose k items from n, look at one particular item. Either you include it, and then choose k − 1 more from the remaining n − 1, or you leave it out and choose all k from the remaining n − 1. These two cases do not overlap, so their counts add. The same idea explains why row n gives the coefficients of (x + y)ⁿ: multiplying (x + y)ⁿ⁻¹ by (x + y) adds each coefficient to its neighbour.
Worked example
- Default input: Rows = 8.
- Start with row 0: 1. Row 1: 1 1. Row 2: 1 2 1. Row 3: 1 3 3 1.
- Keep adding neighbours: row 4 is 1 4 6 4 1, row 5 is 1 5 10 10 5 1, row 6 is 1 6 15 20 15 6 1, row 7 is 1 7 21 35 35 21 7 1.
- Row 8: 1, 1+7 = 8, 7+21 = 28, 21+35 = 56, 35+35 = 70, then the mirror image.
- Result: 1 8 28 56 70 56 28 8 1. Check one entry directly: C(8, 4) = 40320 ÷ (24 × 24) = 70.
How to read the result
The k-th number in row n (counting from 0) is the number of ways to pick k items from n. For example, 56 in row 8 means there are 56 ways to pick 3 people out of 8. Each row is symmetric because choosing k items is the same as leaving out n − k. The row adds up to 2ⁿ; row 8 sums to 256, the number of subsets of 8 items. The middle entries are the largest.
Common mistakes
- Numbering the top row as 1 instead of 0, which shifts every answer by one row.
- Counting the first entry as k = 1 rather than k = 0.
- Expecting small numbers: entries of row 40 exceed 10¹¹.
Frequently asked questions
Why is it called Pascal's triangle?
Blaise Pascal studied it in his Traité du triangle arithmétique (published 1665), but the triangle was known much earlier in China, India and Persia.
What patterns are hidden in the triangle?
The diagonals give the natural numbers and the triangular numbers, and colouring the odd entries produces the Sierpiński triangle.
How do I use it to expand (x + y)⁵?
Take row 5, 1 5 10 10 5 1: x⁵ + 5x⁴y + 10x³y² + 10x²y³ + 5xy⁴ + y⁵.
Sources: NIST Digital Library of Mathematical Functions, §26.3 (dlmf.nist.gov) · Pascal, B., Traité du triangle arithmétique (1665)