Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
We're out of red. Can we make it blue, please? –  stevenvh Aug 28 '12 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 –  murray Aug 28 '12 at 15:08
    
sjdh, and edit bumped this question and caused me to take another look at it. I am wondering, have you considered my answer? I know it came late but I feel that my method has a significant advantage over others posted. Will you take a look? –  Mr.Wizard Apr 15 '13 at 21:14
add comment

6 Answers

up vote 12 down vote accepted

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

share|improve this answer
add comment

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

share|improve this answer
1  
@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 '12 at 14:00
1  
Also discussed here, if you scroll down (with a nice Manipulate). –  programming_historian Aug 28 '12 at 14:12
1  
You noticed, no doubt, that the code makes use of CellularAutomaton. –  David Carraher Aug 28 '12 at 14:20
1  
@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 '12 at 14:29
add comment

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

share|improve this answer
    
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). –  JohnD Dec 3 '12 at 20:47
    
@John, tweaking ImageSize might help here. –  J. M. Apr 15 '13 at 17:35
add comment

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}
share|improve this answer
1  
Thank you. From your examples I learned a lot. –  sjdh Aug 28 '12 at 15:06
    
Glad to be of help. –  David Carraher Aug 28 '12 at 15:09
add comment

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

share|improve this answer
    
Thanks for your answer. Is there any particular reason you like this representation? –  sjdh Aug 29 '12 at 8:03
4  
@sjdh This is the way the Pascal triangle was first historically arranged. I have seen it in this book by Hald. –  Sasha Aug 29 '12 at 12:21
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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