# Variable depth of nesting inside Table [closed]

I need to create a table where there are variable number of iterators.

something like Table[s_1 *s_2 *... s_n,{s_1,1,5},{s_2,1,5},....,{s_n,1,5}] where n is a variable.

How to achieve this in mathematica?

• Are you asking how to produce a template for arbitrary n or do you want to perform calculations with symbolic lists of an arbitrary structure? I think that Mathematica does not have (builtin) capabilities for the latter. Commented Jul 8, 2020 at 19:35
• In other words, do you just need $s_1×s_2×...×s_n$ as 1st argument of Table, or you actually need $f(s_1, s_2, ..., s_n)$? Commented Jul 11, 2020 at 8:47

If I understand your ultimate goal, to produce those $$s_1\ s_2\ ...\ s_n$$ products, try the following instead, which should be equivalent to $$s_i$$ for $$i=1$$ to $$3$$:

n = 3;
Times @@@ Tuples@Table[Range[5], n]


{1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15, 4, 8, 12, 16, 20, 5, 10, 15, 20, 25, 2, 4, 6, 8, 10, 4, 8, 12, 16, 20, 6, 12, 18, 24, 30, 8, 16, 24, 32, 40, 10, 20, 30, 40, 50, 3, 6, 9, 12, 15, 6, 12, 18, 24, 30, 9, 18, 27, 36, 45, 12, 24, 36, 48, 60, 15, 30, 45, 60, 75, 4, 8, 12, 16, 20, 8, 16, 24, 32, 40, 12, 24, 36, 48, 60, 16, 32, 48, 64, 80, 20, 40, 60, 80, 100, 5, 10, 15, 20, 25, 10, 20, 30, 40, 50, 15, 30, 45, 60, 75, 20, 40, 60, 80, 100, 25, 50, 75, 100, 125}

If you want the same list structure that would be generated by Table, then try

ArrayReshape[Times @@@ Tuples@Table[Range[5], n], ConstantArray[5, n]]


{{{1, 2, 3, 4, 5}, {2, 4, 6, 8, 10}, {3, 6, 9, 12, 15}, {4, 8, 12, 16, 20}, {5, 10, 15, 20, 25}}, {{2, 4, 6, 8, 10}, {4, 8, 12, 16, 20}, {6, 12, 18, 24, 30}, {8, 16, 24, 32, 40}, {10, 20, 30, 40, 50}}, {{3, 6, 9, 12, 15}, {6, 12, 18, 24, 30}, {9, 18, 27, 36, 45}, {12, 24, 36, 48, 60}, {15, 30, 45, 60, 75}}, {{4, 8, 12, 16, 20}, {8, 16, 24, 32, 40}, {12, 24, 36, 48, 60}, {16, 32, 48, 64, 80}, {20, 40, 60, 80, 100}}, {{5, 10, 15, 20, 25}, {10, 20, 30, 40,50}, {15, 30, 45, 60, 75}, {20, 40, 60, 80, 100}, {25, 50, 75, 100, 125}}}

One approach might be to define a function

variableNested[n_, f_] := Table[
Evaluate[f @@ Table[s[i], {i, n}]],
Evaluate[Sequence @@ Table[{s[i], 1, 5}, {i, n}]]
]


which you then could call via

variableNested[3, Times]
(* {{{1, 2, 3, 4, 5}, {2, 4, 6, 8, 10}, {3, 6, 9, 12, 15}, {4, 8, 12, 16,
20}, {5, 10, 15, 20, 25}}, {{2, 4, 6, 8, 10}, {4, 8, 12, 16,
20}, {6, 12, 18, 24, 30}, {8, 16, 24, 32, 40}, {10, 20, 30, 40,
50}}, {{3, 6, 9, 12, 15}, {6, 12, 18, 24, 30}, {9, 18, 27, 36,
45}, {12, 24, 36, 48, 60}, {15, 30, 45, 60, 75}}, {{4, 8, 12, 16,
20}, {8, 16, 24, 32, 40}, {12, 24, 36, 48, 60}, {16, 32, 48, 64,
80}, {20, 40, 60, 80, 100}}, {{5, 10, 15, 20, 25}, {10, 20, 30, 40,
50}, {15, 30, 45, 60, 75}, {20, 40, 60, 80, 100}, {25, 50, 75,
100, 125}}} *)

tupleProducts = Times @@@ Tuples[Range[#], #2] &;

tupleProducts[5, 3]

{1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15, 4, 8, 12, 16, 20, 5,
10, 15, 20, 25, 2, 4, 6, 8, 10, 4, 8, 12, 16, 20, 6, 12, 18, 24, 30,
8, 16, 24, 32, 40, 10, 20, 30, 40, 50, 3, 6, 9, 12, 15, 6, 12, 18,
24, 30, 9, 18, 27, 36, 45, 12, 24, 36, 48, 60, 15, 30, 45, 60, 75, 4,
8, 12, 16, 20, 8, 16, 24, 32, 40, 12, 24, 36, 48, 60, 16, 32, 48, 64,
80, 20, 40, 60, 80, 100, 5, 10, 15, 20, 25, 10, 20, 30, 40, 50, 15,
30, 45, 60, 75, 20, 40, 60, 80, 100, 25, 50, 75, 100, 125}

partitionedTP = Partition[tupleProducts[##], #] &;

partitionedTP[5, 3]

{{1, 2, 3, 4, 5}, {2, 4, 6, 8, 10}, {3, 6, 9, 12, 15}, {4, 8, 12, 16, 20},
{5, 10, 15, 20, 25}, {2, 4, 6, 8, 10}, {4, 8, 12, 16, 20}, {6, 12, 18, 24, 30},
{8, 16, 24, 32, 40}, {10, 20, 30, 40, 50}, {3, 6, 9, 12, 15}, {6, 12, 18, 24, 30},
{9, 18, 27, 36, 45}, {12, 24, 36, 48, 60}, {15, 30, 45, 60, 75}, {4, 8, 12, 16, 20},
{8, 16, 24, 32, 40}, {12, 24, 36, 48, 60}, {16, 32, 48, 64, 80},
{20, 40, 60, 80, 100}, {5, 10, 15, 20, 25}, {10, 20, 30, 40, 50},
{15, 30, 45, 60, 75}, {20, 40, 60, 80, 100}, {25, 50, 75, 100, 125}}

partitionedTP2 = Join @@ Outer[Times, ## & @@ ConstantArray[Range[#], #2]] &;

partitionedTP2[5, 3] == partitionedTP[5, 3]

True