New Video: Dynamic Pascal’s Triangle

Made with Mathematica

The top 6 rows of Pascal’s triangle. Each row starts and ends with a “1”. Any number in-between the ends is the sum of the two numbers most close to it on the preceding line.

Pascal’s triangle is an infinite triangle of numbers with many internal patterns.

Start by creating a list of “1’s” in an upside-down V-pattern as seen in the figure above and imagine that this pattern goes on forever and ever to the lower left and lower right.

Next, using the two “1’s” in the second row, add them to get 1+1=2 and place this “2” in the middle of the third row (halfway between the two “1’s” above it).

Next, move on to adding successive numbers in the third row and placing the sums underneath each pair and halfway between them: since 1+2=3, in the fourth row, place a “3” halfway between the “1” and “2”; and since 2+1=3, in the fourth row, place another “3” halfway between the “2” and “1” .

Continue in this manner: add successive numbers in the fourth row and place the sums underneath each pair and halfway between them in the fifth row: 1+3=4, 3+3=6, and 3+1=4.

In the sixth row, the new numbers are 1+4=5, 4+6=10, 6+4=10, and 4+1=5.

Imagine continuing this process forever. Of course, you can’t literally do such a thing. However, given enough time, in theory, you could compute any of the infinitely many rows in this triangle. After all, infinity is really big!

There are many interesting patterns in this triangle, as well as interesting relationships to other parts of mathematics. For example, see if you can find the so-called triangular numbers in Pascal’s triangle.

Before getting into a description of how to use Wolfram Research’s Mathematica computer algebra system to make a dynamic version of this triangle, I will describe one of the most common applications, especially for young people: finding binomial coefficients and using the Binomial Theorem.

Binomial Coefficients

First off, you need to understand that a “binomial” is any mathematical expression of the form a+b, where a and b are symbols that can represent any “numerical quantity”: they could be particular real numbers, complex numbers, variables, or even matrices of the same dimension (they would each have the same number of rows and the same number of columns). Note that this expression has twoterms” (the a and the b) — thus, the name, “binomial”.

For sake of comparison, a “trinomial” is a mathematical expression of the form a+b+c, having three terms. In general, if the number of terms in the expression is not specified, it is called a “polynomial“.

If multiplication of such numerical quantities is “allowed”, then powers of such a binomial can be “expanded”. For example, using the distributive property, we can write: (a+b)^{2}=(a+b)\cdot (a+b)=(a+b)\cdot a+(a+b)\cdot b=a^{2}+b\cdot a+a\cdot b+b^{2}.

If multiplication is commutative, as it is in most “ordinary” number systems (it is not commutative for matrix multiplication), then b\cdot a=a\cdot b so that b\cdot a+a\cdot b=2ab, where in the last step, as is common to do, I have dispensed with using a dot \cdot for multiplication and am just using “juxtaposition” (“implied multiplication”).

Therefore, if multiplication is commutative, (a+b)^{2}=a^{2}+2ab+b^{2}=1\cdot a^{2}+2\cdot ab+1\cdot b^{2}. In the last step, I went back to using a dot \cdot because I wanted to emphasize the “coefficients” of the answer. Notice that these coefficients are the same as the numbers in the third row of Pascal’s triangle. This is no accident.

Let’s see if the pattern continues. Expand the cube of a binomial: (a+b)^{3}=(a+b)\cdot (a+b)^{2}=(a+b)\cdot (a^{2}+2ab+b^{2}). By the distributive property, we see that (a+b)^{3}=(a+b)\cdot a^{2}+(a+b)\cdot 2ab+(a+b)\cdot b^{2}, so (a+b)^{3}=a^{3}+ba^{2}+2a^{2}b+2ab^{2}+ab^{2}+b^{3}=a^{3}+3a^{2}b+3ab^{2}+b^{3}. Notice that this is the same as 1\cdot a^{3}+3\cdot a^{2}b+3\cdot ab^{2}+1\cdot b^{3}, and that these coefficients are the same as the numbers in the fourth row of Pascal’s triangle.

For a fourth power, you should take the time to check that the answer is: (a+b)^{4}=1\cdot a^{4}+4\cdot a^{3}b+6\cdot a^{2}b^{2}+4\cdot ab^{3}+1\cdot b^{4}.

In general, to “expand” (a+b)^{n} for n=0,1,2,\ldots, use the numbers from row n+1 of Pascal’s triangle as the coefficients. Since a+b is a binomial, these numbers can be called “binomial coefficients“.

There are a few different special notations for these numbers. For an integer n=0,1,2,3,\ldots, and another integer k=0,1,2,\ldots,n, the (k+1)^{\mbox{st}} number in the (n+1)^{\mbox{st}} row of Pascal’s triangle is mostly commonly denoted by the two notations _{n}C_{k} and \left(\begin{array}{c} n \\ k \\ \end{array}\right).

For example, _{3}C_{0}=\left(\begin{array}{c} 3 \\ 0 \\ \end{array}\right)=1, _{5}C_{2}=\left(\begin{array}{c} 5 \\ 2 \\ \end{array}\right)=10, and _{4}C_{4}=\left(\begin{array}{c} 4 \\ 4 \\ \end{array}\right)=1.

It turns out that these numbers are also of foundational importance in the field of combinatorics (part of discrete mathematics) and that _{n}C_{k}=\left(\begin{array}{c} n \\ k \\ \end{array}\right)=\frac{n!}{k!(n-k)!}, where the exclamation point “!” represents the factorial function (n!=n\cdot (n-1)\cdot (n-2)\cdot 3\cdot 2\cdot 1 when n=1,2,3,\ldots, and 0!=1, by definition). They occur with the fundamental idea of a “combination” of k objects chosen from a group of n objects, and the quantity _{n}C_{k} is often called “n choose k“.

Binomial Theorem

With this notation in hand, we can write the following equation for any integer n=0,1,2,\ldots, and it represents the conclusion of the Binomial Theorem:

(a+b)^{n}=\displaystyle\sum_{k=0}^{n}\left(\begin{array}{c}n\\k\\ \end{array}\right)a^{n-k}b^{k}

=\left(\begin{array}{c}n\\0\\ \end{array}\right)a^{n}+\left(\begin{array}{c}n\\1\\ \end{array}\right)a^{n-1}b+\left(\begin{array}{c}n\\2\\ \end{array}\right)a^{n-2}b^{2}+\cdots+\left(\begin{array}{c}n\\n\\ \end{array}\right)b^{n}.

A Dynamic Version of Pascal’s Triangle in Mathematica

The video in the title of this post is embedded below. In it, I describe how to create a dynamic version of Pascal’s triangle in Mathematica. The dynamic aspect refers to the number of rows of Pascal’s triangle that are displayed in the output. This can be changed by the user with a slider.

The first step to creating a dynamic (changeable) version of Pascal’s triangle in Mathematica is to create a static (unchangeable) version; and the first steps in creating a static version is to understand how to use Mathematica to compute binomial coefficients and to make “lists”.

Mathematica comes with many built-in “functions” or “commands”. One of them is “Binomial“. The syntax is Binomial[n,k]. If you enter this, with specific numbers plugged in for n and k, then the output will be the binomial coefficient _{n}C_{k}=\left(\begin{array}{c} n \\ k \\ \end{array}\right). For example, the input Binomial[7,4] returns an output of \frac{7!}{4!(7-4)!}=\frac{5040}{24\cdot 6}=35.

Next, we want to know how to create a “list” of such values. A “list” in Mathematica is an expression of the form {a,b,c,…}, where the finitely-many a, b, c, etc… can be any kinds of objects, including numbers, matrices, polynomials, graphs, and even other lists. The most fundamental way to create a list using a “formula” is with the Table function. The general syntax Table[expression,{i,imin,imax}] generates a “list” of the values of expression as i runs from imin to imax in increments of 1 unit at a time.

For example, Table[i^2,{i,4,7}] will generate the list {16,25,36,49} of squares of 4, 5, 6, and 7 as output.

An example relevant for the task of creating Pascal’s triangle is the input Table[Binomial[5,k],{k,0,5}]. This generates the list \{_{5}C_{0},_{5}C_{1},_{5}C_{2},_{5}C_{3},_{5}C_{4},_{5}C_{5}\}=\{1,5,10,10,5,1\} which consists of the numbers in the sixth row of Pascal’s triangle.

We can even use Table in a nested way, either as Table[Table[Binomial[n,k],{k,0,5}],{n,0,5}], or more simply as Table[Binomial[n,k],{n,0,5},{k,0,5}] to generate a list of lists: {{1,0,0,0,0,0},{1,1,0,0,0,0},{1,2,1,0,0,0},{1,3,3,1,0,0},{1,4,6,4,1,0},{1,5,10,10,5,1}}, which includes all of the first six rows of Pascal’s triangle, plus some additional zeros (by “default”, Binomial[n,k] gives an output of 0 when n < k).

How can we get rid of the zeros? By making sure that, for each value of n, the value of k only varies from 0 to n (rather than 0 to 5). The input Table[Binomial[n,k],{n,0,5},{k,0,n}] will generate the output {{1},{1,1},{1,2,1},{1,3,3,1},{1,4,6,4,1},{1,5,10,10,5,1}}, which excludes the zeros.

The only thing left to do to create a static version of Pascal’s triangle is to “stack” these lists in a “column” and “center” them. This is accomplished with the function Column and the option Center with the following syntax: Column[Table[Binomial[n,k],{n,0,5},{k,0,n}],Center].

In fact, this last bit of code was used to generate the figure at the start of this post.

Finally, we need to make the output dynamic. We would like to allow the user to move a slider to change the number of rows. The key Mathematica function to accomplish this feat is Manipulate. In general, the syntax Manipulate[expression,{u,umin,umax}] will generate output animating the “values” of expression (which don’t have to be numbers), as the “animation parameter” u varies in a “continuous” way from umin to umax. If we want the output to be animated in a “discrete” way as u varies from umin to umax in increments of 1, we can modify the syntax to be Manipulate[expression,{u,umin,umax,1}]. We can also add the option LabelStyle -> Large at the very end so that the slider label (the animation parameter) will be “large”.

For the task at hand, the relevant code that will generate the first 11 rows in a dynamic fashion is: Manipulate[Column[Table[Binomial[n,k],{n,0,rows-1},{k,0,n}],Center],{rows,1,11,1},LabelStyle->Large]. Notice that the animation parameter here is “rows“.

We end with a gif of the output of this code:

Dynamic Pascal’s Triangle, made with Mathematica