# Visualize Pascal's triangle and other triangle shaped lists

How can Pascal's triangle be visualised like this in Mathematica? Or more generally, how can a 'triangular' list like

{{1},{1, 1}, {1, 2, 1}}


be visualized in this way.

Also I would like to do 'conditional things' like colouring the number two red.

• We're out of red. Can we make it blue, please? Aug 28, 2012 at 13:57
• But should Pascal's triangle be displayed like that, with each row center-aligned -- or would it be better to have all the rows left-aligned? In the latter form, it's often easier to calculate with it. That's an old insight by Ken Iverson; see, e.g.: jsoftware.com/jwiki/Essays/Pascal's%20Triangle Aug 28, 2012 at 15:08

Here is another way:

pascalTriangle[n_] :=
NestList[{1, Sequence @@ Plus @@@ Partition[#, 2, 1], 1} &, {1},
n - 1];

Column[Grid[{#}, ItemSize -> 3] & /@ (pascalTriangle /.
x_Integer :>
Text[Style[x, Large, If[x == 2, Red, Black]]]), Center] t = Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}]

(*out *)
{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1},
{1, 6, 15, 20, 15, 6, 1}, {1, 7, 21, 35, 35, 21, 7, 1}, {1, 8, 28, 56, 70, 56, 28, 8, 1}}


You can format the list as a matrix:

MatrixForm[t] You can insert tabs between the items and print each row, literally as a Row, in a grid.

{Row[#, "\t"]} & /@ t // Grid By using Row, we are sending Grid one item for each gridrow. (If Row were not employed, Grid would treat each item of a sublist as requiring its own column. That will lead to a triangle skewed from left to right. )

Here are a couple of ideas on how to style the 2 as large and red. The following will make any entry of 2 red (it will not color the 2 in 20).

t1 = Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}] /. {2 -> Style[2, Red, 18]} You may then format t1 as a matrix or as a grid.

The following makes the 2 in the center of a sublist with 3 elements red. It's not necessary to do this because 2 only shows up once in Pascal's triangle. But you get the idea...

t2 = Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}] /. {a_, 2, c_} :> {a, Style[2, Red, 18], c}

• Thank you. From your examples I learned a lot.
– sjdh
Aug 28, 2012 at 15:06
• Glad to be of help. Aug 28, 2012 at 15:09

My modest attempt:

With[{n = 7},
Graphics[Table[Text[Style[Binomial[n - j, n - i], Large], {Sqrt (i - j/2), 3 j/2}],
{i, n}, {j, i}]]] Here's a more general function:

triangularArrayLayout[triArray_List, opts___] := Module[{n = Length[triArray]},
Graphics[MapIndexed[
Text[Style[#1, Large], {Sqrt (n - 1 + #2.{-1, 2}), 3 (n - First[#2] + 1)}/2] &,
triArray, {2}], opts]]


Use it on the Stirling subset numbers $\left\{{n \atop k}\right\}$:

triangularArrayLayout[Table[StirlingS2[n, k], {n, 0, 5}, {k, 0, n}]] • How can I control the horizontal spacing between elements within a row here? (I tried using this method for something else and everything worked fine except the entries are quite cramped). Dec 3, 2012 at 20:47
• @John, tweaking ImageSize might help here. Apr 15, 2013 at 17:35

This is my favorite way to draw Pascal's triangle.

pt = NestList[{0, ##} + {##, 0} & @@ # &, {1}, #] &;

ptform[pt : {_List ..}] :=
With[{n = Length@Last@pt, long = Max@Map[StringLength@ToString@# &, pt, {2}]},
Graphics[MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, pt, {2}],
PlotRange -> All, AspectRatio -> 0.7,
BaseStyle -> FontSize -> Scaled[1.5/(n long)]]]


The advantage here is that the produced graphic is resizable, and the font size is automatically selected. Elements can be styled either with /. or MapAt.

pt@7 /. 2 -> Style[2, Red] // ptform MapAt[Style[#, Red] &, pt@11, {{5, 3}, {6, 4}, {7, 1}}] // ptform I like the following way of visualizing the Pascal triangle:

PascalTriangleForm[li : {{_}, {_, _}, __List}] /;
(Length /@ li) == Range[Length[li]] :=
Level[Table[Table[{i, n + 1 - i}, {i, 1, n}], {n,Length[li]}], {2}] ->
Level[li, {2}]
], Automatic, ""]] • Thanks for your answer. Is there any particular reason you like this representation?
– sjdh
Aug 29, 2012 at 8:03
• @sjdh This is the way the Pascal triangle was first historically arranged. I have seen it in this book by Hald. Aug 29, 2012 at 12:21
• I like this as well, easier to store in a grid. Aug 29, 2016 at 23:27

Intuitive noobish way to program this.

Table[CoefficientList[(x + 1)^i, x], {i, 0, 10}]


I hope this helps other beginners :)

use MatrixForm to visualise! Another way is to use CellularAutomaton:

pascal = CellularAutomaton[{#.{1, 0, 1} &, {}, 1}, {{1}, 0}, 6];

Text[Grid[pascal /. {0 -> ""}, ItemSize -> {1.5, 1.5}, Spacings -> {0, 0}]] This is actually demonstrated on the Wolfram Demonstrations Project pages. Download the notebook!

• @sjdh and just so you know: I didn't happen to know that it was part of the Wolfram Demonstrations. I used a search engine to find that out.
– F'x
Aug 28, 2012 at 14:00
• Also discussed here, if you scroll down (with a nice Manipulate). Aug 28, 2012 at 14:12
• You noticed, no doubt, that the code makes use of CellularAutomaton. Aug 28, 2012 at 14:20
• @F'x This Wolfram demonstration I have seen before, I'm sure there are simpler ways to draw Pascal's triangle that can be used to visualise any list with a triangle shape.
– sjdh
Aug 28, 2012 at 14:29

Another way to generate Pascal's triangle is to use a kind of cellular automaton on a rectangular grid, starting with a zero grid with one $1$ at the top and the rule at each step makes a zero cell into a sum of its above diagonal neighbors.

The code is:

Nm=12;
C1=Table[0,{j,1,Nm},{k,1,2Nm}];
C1[[1,Nm]]=1;
C2=C1;
Do[Do[If[C2[[j,k]]==0,C2[[j,k]]=C1[[j-1,k+1]]+C1[[j-1,k-1]]],{j,2,Nm-1},{k,2,2Nm-1}];
C1=C2,{n,1,Nm}]
Print[Grid[Table[If[C2[[j,k]]==0," ",C2[[j,k]]],{j,1,Nm},{k,1,2Nm}]]]


The result is: f[m_, k_] := If[Binomial[m, k] != 0, Binomial[m, k], ""]

pascal[n_] := Grid[Table[f[m, k], {m, 0, n}, {k, 0, n}]] • It is not really different from: mathematica.stackexchange.com/a/9963/5478, is it?
– Kuba
Mar 9, 2018 at 8:51
• No it's not, just slightly different look Mar 9, 2018 at 8:54