4
$\begingroup$

This problem relates to Prepend 0 to sublists I have the list

t1 = Table[m = n + IntegerReverse[n]; n = m, {n, 3}, {i, 1, 4}]

giving

{{2, 4, 8, 16}, {4, 8, 16, 77}, {6, 12, 33, 66}}

But I want to start with the first value n so I should get

{{1, 2, 4, 8, 16}, {2, 4, 8, 16, 77}, {3, 6, 12, 33, 66}}

It can be done by

Prepend[#, #[[1]]/2] & /@ t1

But this is cheatingly awkward. How can I do it properly or within the Table command? I tried Hold and Prepend inside Table but did not get anywhere.

$\endgroup$
1
  • $\begingroup$ Flatten /@ Thread[{Range[3], t1}] seems natural enough. $\endgroup$
    – LouisB
    Feb 22, 2020 at 5:53

1 Answer 1

4
$\begingroup$
MapIndexed[Join[#2, #] &, Table[n = n + IntegerReverse[n], {n, 3}, {i, 4}]]

{{1, 2, 4, 8, 16}, {2, 4, 8, 16, 77}, {3, 6, 12, 33, 66}}

To get the same result using only Table:

Table[n = If[i == 0, n, n + IntegerReverse[n]], {n, 3}, {i, 0, 4}]

{{1, 2, 4, 8, 16}, {2, 4, 8, 16, 77}, {3, 6, 12, 33, 66}}

$\endgroup$

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.