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.

I want to put edges on this Nicomachus's Triangle

r = 7;
t = Table[2^(n - k) 3^k, {n, 0, r}, {k, 0, n}];
ColumnForm[t, Center]

Is there an easy way to do this?

Using Mr. Wizard's method

triangleForm[t : {_List ..} /; Depth@t == 3] :=
 Show[Graphics[{
    Red,
    (Line /@ Join[#, Riffle @@@ Partition[#, 2, 1]]) &@
      Table[{(1 - i + 2 j - r)/2, i - r - 1}, {i, 0, r}, {j, i, r}],
    MapIndexed[Text[Panel[#, FrameMargins -> 0], {#2 - #/2, -#} & @@ #2] &,     t, {2}]
   }],
  TextStyle -> FontSize -> 18
 ]

triangleForm[t]

NicomachusTriangle

http://math.stackexchange.com/questions/150259/question-about-the-collatz-conjecture-nicomachuss-triangle-and-more

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

Something like this?

triangleForm[t : {_List ..} /; Depth@t == 3] :=
 Show[Graphics[{
    MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, t, {2}],
    Red,
    Line[{{1/2, -1}, {1 - #/2, -#}, {#/2, -#}, {1/2, -1}}] & @ Length[t]
   }],
  TextStyle -> FontSize -> 18]

triangleForm[t]

Mathematica graphics

Or probably more like:

triangleForm[t : {_List ..} /; Depth@t == 3] :=
 Show[Graphics[{
    MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, t, {2}],
    Red,
    Line[{{1/2, 0}, {-#/2, -# - 1/2}, {1 + #/2, -# - 1/2}, {1/2, 0}}] & @ Length[t]
   }],
  TextStyle -> FontSize -> 18]

triangleForm[t]

Mathematica graphics


With automatic image size:

r = 9;
t = Table[2^(n - k) 3^k, {n, 0, r}, {k, 0, n}];

triangleForm[t : {_List ..} /; Depth@t == 3] :=
 Show[Graphics[{
    MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, t, {2}],
    Red,
    Line[{{1/2, 0}, {-#/2, -# - 1/2}, {1 + #/2, -# - 1/2}, {1/2, 0}}] & @ Length[t]
   }],
  TextStyle -> FontSize -> 16,
  ImageSize -> (Length@t + 1) * First@Rasterize[Style[t[[-1, -1]], 16], "RasterSize"]
 ]

triangleForm[t]

Mathematica graphics


Another variation following a comment below:

r = 8;
t = Table[2^(n - k) 3^k, {n, 0, r}, {k, 0, n}];

triangleForm[t : {_List ..} /; Depth@t == 3] :=
 Show[Graphics[{
    Red,
    (Line /@ Join[#, Riffle @@@ Partition[#, 2, 1]]) &@
      Table[{(1 - i + 2 j - r)/2, i - r - 1}, {i, 0, r}, {j, i, r}],
    MapIndexed[Text[Panel[#, FrameMargins -> 0], {#2 - #/2, -#} & @@ #2] &, t, {2}]
   }],
  TextStyle -> FontSize -> 18
 ]

triangleForm[t]

Mathematica graphics

share|improve this answer
    
@belisarius are you making the graphic large enough for all the numbers? –  Mr.Wizard May 27 '12 at 1:49
    
Can you pre-calculate the image size? :D –  belisarius May 27 '12 at 1:52
    
@belisarius I suppose so; let me try –  Mr.Wizard May 27 '12 at 1:52
    
@Mr.Wizard, like the first one, but with all the inner triangles shown also. –  Fred Kline May 27 '12 at 2:09
    
@Fred okay, give me a minute. :-) –  Mr.Wizard May 27 '12 at 2:18
show 4 more comments

Here's an alternative way to create the latter picture in Mr.Wizard's answer using Graph:

elem[n_, k_] := 2^(n - k) 3^k

Block[{e, r},
 r = 7;
 vertices = Flatten[Table[e[n, k], {n, 0, r}, {k, 0, n}]];
 edges = Flatten[{
     Table[e[n, k] \[UndirectedEdge] e[n + 1, k], {n, 0, r - 1}, {k, 0, n}],
     Table[e[n, k] \[UndirectedEdge] e[n + 1, k + 1], {n, 0, r - 1}, {k, 0, n}],
     Table[e[n, k] \[UndirectedEdge] e[n, k + 1], {n, 1, r}, {k, 0, n - 1}]}];
 coords = Flatten[Table[{-n/2 + k, r - n}, {n, 0, r}, {k, 0, n}], 1];

 Graph[vertices, edges, VertexCoordinates -> coords,
  EdgeStyle -> Directive[Thick, Red],
  VertexShapeFunction -> ({Black, 
      Inset[Framed[Style[#2 /. e -> elem, 12, FontFamily -> "Helvetica"], 
        Background -> White, RoundingRadius -> 3], #1]} &)]
]

Mathematica graphics

share|improve this answer
    
That is what I was trying to do before I gave up and asked the question. I've added it to my .nb –  Fred Kline May 27 '12 at 10:30
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.