/\ Pascal’s Triangle /\

Gerardmistretta
3 min readMay 3, 2021

Sticking with last week’s theme of shapes, I thought it would be fitting to discuss another commonly-practice algorithm involving a different triangle
— Pascal’s Triangle. “Pascal’s triangle is a triangular array of the binomial coefficients that arises in probability theory, combinatorics, and algebra.”

The first row of Pascal’s Triangle always starts with the number 1. Coincidently, the first and last numbers in every row of the triangle are also 1. Each row is built by summing up the adjacent elements from the previous row.

Now one cool fact about Pascal’s triangle is that it is completely symmetrical. Regardless of how many rows you give it, the triangle is a mirror image of itself if split down the middle. Another fun tidbit of information is that the triangle is infinite; meaning it can go on forever.

The triangle was named after a French Mathematician named Blaise Pascal. Blaise died before the age of forty, but he was a prodigy who significantly influenced projective geometry, probability theory, and social science.

Blaise Pascal was a French mathematician, physicist, inventor, philosopher, writer and Catholic theologian

But enough fun facts and history, let’s channel our inner Bob and get to Building!

To start, you’re going to want to create a variable specifying the number of rows you want; as well as an array that accepts that number of rows:

let totalRows = 6
let pascalsTriangle = new Array(totalRows)

Next, you are going to want to iterate through the total amount of rows with a simple for loop. Every time you go through the loop, you will want to create a new row. You will also want to set the first and last numbers of each row. Like I mentioned above, they will always be equal to 1.

for(let i = 0; i < totalRows; i++) {
let row = new Array(i + 1);
row[0] = 1;
row[row.length - 1] = 1;

Now, this is where things get a little tricky. At this point, you will need to generate the values for each row. You know, the numbers in between the starting(1) and ending(also 1) numbers of each row. The best way to do this, or at least the way that made the most sense to me, was to use another for loop. However, you will want to set “j” equal to 1 because the first index of the row has already been determined(again, it is 1). This loop will increment “j” until the entire row is completed.

To determine the values, we need access to the numbers in the previous row. You can simply set a variable equal to the previous row by calling the entire array at that specific point, like so:

for(let j = 1; j < row.length - 1; j++) {
let prevRow = pascalsTriangle[i - 1];
row[j] = prevRow[j] + prevRow[j - 1];
}

What row[j] is doing is accessing “j” and the number before “j” in the previous row, and adding them together to determine the proper value of each index.

Finally, you are going to want to set each index of Pascal’s Triangle equal to that specific row. Once this is done, return your Array and you are finished!

For more information on Pascal’s Triangle, as well as other ways to create it, see the links below.

--

--