36
$\begingroup$

How can Pascal's triangle be visualised like this in Mathematica? enter image description here

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.

$\endgroup$
2
  • 3
    $\begingroup$ We're out of red. Can we make it blue, please? $\endgroup$
    – stevenvh
    Aug 28, 2012 at 13:57
  • $\begingroup$ 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 $\endgroup$
    – murray
    Aug 28, 2012 at 15:08

10 Answers 10

27
$\begingroup$

Here is another way:

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

Column[Grid[{#}, ItemSize -> 3] & /@ (pascalTriangle[7] /. 
    x_Integer :> 
     Text[Style[x, Large, If[x == 2, Red, Black]]]), Center]

enter image description here

$\endgroup$
29
$\begingroup$

To address your question about visualizing a triangular list, let's use the following list:

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]

matrix


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

{Row[#, "\t"]} & /@ t // Grid

tabs

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]}

out red

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}
$\endgroup$
2
  • 2
    $\begingroup$ Thank you. From your examples I learned a lot. $\endgroup$
    – sjdh
    Aug 28, 2012 at 15:06
  • $\begingroup$ Glad to be of help. $\endgroup$
    – DavidC
    Aug 28, 2012 at 15:09
27
$\begingroup$

My modest attempt:

With[{n = 7},
     Graphics[Table[Text[Style[Binomial[n - j, n - i], Large], {Sqrt[3] (i - j/2), 3 j/2}],
                    {i, n}, {j, i}]]]

Pascal's triangle


Here's a more general function:

triangularArrayLayout[triArray_List, opts___] := Module[{n = Length[triArray]}, 
  Graphics[MapIndexed[
         Text[Style[#1, Large], {Sqrt[3] (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}]]

Stirling subset triangle

$\endgroup$
2
  • 1
    $\begingroup$ 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). $\endgroup$
    – JohnD
    Dec 3, 2012 at 20:47
  • $\begingroup$ @John, tweaking ImageSize might help here. $\endgroup$ Apr 15, 2013 at 17:35
17
$\begingroup$

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

Mathematica graphics

MapAt[Style[#, Red] &, pt@11, {{5, 3}, {6, 4}, {7, 1}}] // ptform

Mathematica graphics

$\endgroup$
14
$\begingroup$

I like the following way of visualizing the Pascal triangle:

PascalTriangleForm[li : {{_}, {_, _}, __List}] /; 
   (Length /@ li) == Range[Length[li]] :=   
 Grid[SparseArray[Thread[
    Level[Table[Table[{i, n + 1 - i}, {i, 1, n}], {n,Length[li]}], {2}] -> 
    Level[li, {2}]
 ], Automatic, ""]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for your answer. Is there any particular reason you like this representation? $\endgroup$
    – sjdh
    Aug 29, 2012 at 8:03
  • 10
    $\begingroup$ @sjdh This is the way the Pascal triangle was first historically arranged. I have seen it in this book by Hald. $\endgroup$
    – Sasha
    Aug 29, 2012 at 12:21
  • $\begingroup$ I like this as well, easier to store in a grid. $\endgroup$
    – aarti
    Aug 29, 2016 at 23:27
6
$\begingroup$

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! enter image description here

$\endgroup$
6
$\begingroup$

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}]]

enter image description here

$\endgroup$
3
$\begingroup$

This is actually demonstrated on the Wolfram Demonstrations Project pages. Download the notebook!

$\endgroup$
4
  • 2
    $\begingroup$ @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. $\endgroup$
    – F'x
    Aug 28, 2012 at 14:00
  • 1
    $\begingroup$ Also discussed here, if you scroll down (with a nice Manipulate). $\endgroup$ Aug 28, 2012 at 14:12
  • 1
    $\begingroup$ You noticed, no doubt, that the code makes use of CellularAutomaton. $\endgroup$
    – DavidC
    Aug 28, 2012 at 14:20
  • 1
    $\begingroup$ @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. $\endgroup$
    – sjdh
    Aug 28, 2012 at 14:29
2
$\begingroup$

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:

enter image description here

$\endgroup$
1
$\begingroup$
f[m_, k_] := If[Binomial[m, k] != 0, Binomial[m, k], ""]

pascal[n_] := Grid[Table[f[m, k], {m, 0, n}, {k, 0, n}]]

enter image description here

$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.