Create a multiplication table using AppendTo/Drop?

clear[Num4];
Num4 = Table[{i*j}, {i, 1, 12, 1}, {j, 1, 12, 1}];
Num4 // TableForm

I am trying to produce a proper multiplication table; however, the current code only produces the output for the table and does not show the actual rows/columns listed as 1,2..12.

I know I need to use AppendTo but cannot figure out exactly what to do. Any help is appreciated.

Is this what you mean?

TableForm[
 Table[{i*j}, {i, 1, 12, 1}, {j, 1, 12, 1}],
 TableHeadings -> {Range[12], Range[12]}
 ]

Benjamin,

This is exactly what I meant. I tried to use the command AppendTo, but I was having some trouble and ended up creating more code than I needed. It produced the same table but was very tedious…I am a novice in Mathematica but picking up on things quickly..

This is how I went about it, like I said very tedious. If you can use one line of code vs ten lines of code then I would always choose one line of course..

clear[Num4];

Num4 = Table[{i*j}, {i, 1, 12, 1}, {j, 1, 12, 1}];
PrependTo[Num4, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}];
PrependTo[Num4[[1]], 0];
PrependTo[Num4[[2]], 1];
PrependTo[Num4[[3]], 2];
PrependTo[Num4[[4]], 3];
PrependTo[Num4[[5]], 4];
PrependTo[Num4[[6]], 5];
PrependTo[Num4[[7]], 6];
PrependTo[Num4[[8]], 7];
PrependTo[Num4[[9]], 8];
PrependTo[Num4[[10]], 9];
PrependTo[Num4[[11]], 10];
PrependTo[Num4[[12]], 11];
PrependTo[Num4[[13]], 12];

Like I said, very tedious and almost pointless, although it DID give me the result I wanted, but time is of essence for things like this. I don’t even know how to properly include my code in this post so it looks nice and not messy like what it looks like now.

Thank you for your help. I appreciate it.

Brandon

Hi Brandon,

No problem, I see the approach you’re taking. Consider:

Begin with the table as before,

table = Table[i j, {i, 1, 12}, {j, 1, 12}];
% // TableForm

Then insert, using Transpose (very useful function), a column of numbers 1 to 12

(* insert a column *)
table = Transpose[Insert[Transpose[table], Range[12], 1]];
% // TableForm

Lastly, insert the row.

(* Insert a row *)
PrependTo[table, Range[0, 12]];
table // TableForm

Thanks,
Ben

You can simplify that code a bit:

TableForm[
 Table[i j, {i, 12}, {j, 12}],
 TableHeadings -> Automatic
 ]

If you want to insert those elements explicitly, consider ArrayFlatten.

tab = Table[i j, {i, 12}, {j, 12}];

ArrayFlatten[
 {{0, {Range[12]}},
  {List /@ Range[12], tab}}
 ]

If you are new to Mathematica, think twice before using AppendTo/PrependTo as they modify the value of a variable. The first argument must be a variable. Such repeated modification is often not the best approach in Mathematica. The usual way to do things is with functions that return results instead of modify their arguments in place. In other words, prefer functions that have no side effects as these can be composed together easily.

Of course AppendTo does have a very useful role. Just make sure it’s what you really need.

Another method :

Grid[Table[  a*b, {a, 1, 12}, {b, 1, 12}], {Frame -> {{Left}, {Top}}}]

OR

Grid[Table[  a*b, {a, 1, 12}, {b, 1, 12}], {Dividers -> {{None, 1}, {None, 1}, }}]

Yet another version:

Table[i j, {i, 12}, {j, 12}] //
TableForm[#, TableAlignments -> Right, TableHeadings -> Automatic] & //
Framed[#, FrameStyle -> {Thick, Blue}] &

Thank you everyone!

The best part about coding is that there is SO many ways to get the same process done.

I really appreciate you all!

Brandon

I like the output of this one better.

Here is a multiplication table generator function that is a bit smarter. And, its also fun to see what happens when you place different number forms in there. For example, if you pass in the first number with a decimal point only all output shall have a decimal. This does not hold if you pass in the second number with a decimal point. Also, be careful when passing exponents. You just might end up with a crazy large table.

While a utility function probably exists somewhere in M, yet I do not know what such function is called. While I welcome enhancements and optimizations, the formatting is my own personal style so please feel free to ignore it in favor of your own conventions.

Edit; Now handles inverted ranges.

genMultiTable[low_, high_] := Module[ 
  {x , y, l, h, s, lh, hh},
  (* Later expand functionality by handling options *)
  (* 
  Needs two vars otherwise the headings do not work *)
  
  s = If[high < low
    , -1
    , 1
    ]; 
  l = low;
  h = high;
  x = y = low;
  lh = hh = Range[low, high, s];
  Table[x*y, {x, l, h, s}, {y, l, h, s}]
    // TableForm[# , TableAlignments -> Right, 
      TableHeadings -> {lh, hh} ] & 
    // Framed[#, FrameStyle -> {Thick, Blue} ] &
  ]

David,

This is your function, slimmed down a bit:

genMultiTable2[low_, high_] := Module[
  {s, r},
  s = If[high < low, -1, 1];
  r = Range[low, high, s];
  Table[x*y, {x, low, high, s}, {y, low, high, s}]
  // TableForm[#, TableAlignments -> Right, TableHeadings -> {r, r}] &
  // Framed[#, FrameStyle -> {Thick, Blue}] &
]

Thanks much. I’m pretty new to Mathematica. So this is the best way to learn!