Pascal’s triangle
Given a row count n, return the first n rows of Pascal’s triangle as an array of arrays. Counting rows from 0, row i holds i + 1 numbers. The first and last number of every row is 1, and every number between them is the sum of the two directly above it.
Examples
Example 1
- Input
- n = 6
- Output
[1] [1,1] [1,2,1] [1,3,3,1] [1,4,6,4,1] [1,5,10,10,5,1]
Row 4 reads 1, 4, 6, 4, 1 — the 6 is the 3 and 3 above it added.[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]
Example 2
- Input
- n = 1
- Output
[1]
One row, and with nothing above it both of its ends are the same single[[1]]1.
The Code
function pascalsTriangle(n) {
const triangle = [];
for (let row = 0; row < n; row++) {
const line = [];
for (let col = 0; col <= row; col++) {
if (col === 0 || col === row) {
line.push(1);
} else {
const above = triangle[row - 1];
line.push(above[col - 1] + above[col]);
}
}
triangle.push(line);
}
return triangle;
}
pascalsTriangle(6);The first 19 calls, of 35. This one does not fit on a page.
Explanation
A row is not worked out from scratch; it is read off the row before it. Each number stands under a pair and takes that pair’s total. Only the two ends have a single number above them, so they are the only positions needing a rule of their own.
- 1
rowis 0, so the inner loop runs atcol0 only.col === 0is true,1is pushed, and theelsebranch — the one that reads the row above — is never reached.trianglenow has something for the next row to read.10row=0triangle.length=0
triangle.push([1])
- 2
rowis 1, and both of its positions hit a guard:col === 0at the left andcol === rowat the right. Both take1, so this row is built without opening the one above it either.101 11row=1triangle.length=1
triangle.push([1, 1])
- 3
rowis 2, andcol1 is the first position that is neither end.aboveis set totriangle[row - 1], which is[1, 1], and the value pushed isabove[0] + above[1].101 111 2 12row=2above=[1, 1]
above[0] + above[1] = 1 + 1 → 2
- 4
rowis 3 andaboveis[1, 2, 1]. Position 1 takesabove[0] + above[1]= 1 + 2, position 2 takesabove[1] + above[2]= 2 + 1. Each position reads the pair it sits between, and the ends take their 1 as always.101 111 2 121 3 3 13row=3above=[1, 2, 1]
triangle.push([1, 3, 3, 1])
- 5
rowis 4 andaboveis[1, 3, 3, 1]: 1 + 3, then 3 + 3, then 3 + 1. The 6 in the middle is the only place the two 3s meet.101 111 2 121 3 3 131 4 6 4 14row=4above=[1, 3, 3, 1]
triangle.push([1, 4, 6, 4, 1])
- 6
rowis 5 andaboveis[1, 4, 6, 4, 1], giving 5, 10, 10 and 5 between the two 1s.row++then failsrow < nand the six rows are returned.101 111 2 121 3 3 131 4 6 4 141 5 10 10 5 15row=5above=[1, 4, 6, 4, 1]
return triangle → 6 rows
More like this
All star & number patterns examples (12) →- Star triangle The inner loop runs as far as the row number — that is the whole idea.
- Inverted triangle Same triangle, counted downwards instead of up.
- Star pyramid Leading spaces do the centring; stars go up in odd numbers.
- Star diamond A pyramid, then the same pyramid upside down — minus the shared row.
- Hollow square Fill only the border — every interior cell is a space.
- Butterfly Two wings of stars with a gap between them that closes and reopens.