2
$\begingroup$

Suppose I want Mathematica to output

Table[2^n, {n, 1, 5}]

Mathematica gives

{2, 4, 8, 16, 32}

but how can I get Mathematica to output

{2, 2^2, 2^3, 2^4, 2^5}

?

EDIT

What about more complicated expressions like:

Table[n^2, {n, 1, 5}]

Mathematica gives

{1, 4, 9, 16, 25}.

If I try

Table[HoldForm@n^2, {n, 1, 5}]

I get the unexpected (to me)

{n^2, n^2, n^2, n^2, n^2}
$\endgroup$
  • 2
    $\begingroup$ Table[HoldForm@2^n, {n, 1, 5}] !Mathematica graphics $\endgroup$ – Nasser Oct 1 '14 at 20:44
  • 4
    $\begingroup$ Inactivate[Table[2^n, {n, 1, 5}], Power] (I am not obsessed with Inactive I promise) $\endgroup$ – chuy Oct 1 '14 at 20:53
  • 1
    $\begingroup$ @chuy That's an elegant answer - much more than a comment. $\endgroup$ – eldo Oct 1 '14 at 21:00
  • $\begingroup$ To output Table[2^n, {n, 1, 5}] simply type "Table[2^n, {n, 1, 5}]" and hit <shift>+<return> $\endgroup$ – DavidC Oct 2 '14 at 0:04
  • $\begingroup$ Thank you @ Nasser, @ chuy, @ David Carraher, @kguler. Your comments and answers have helped me a lot, and I was not able to find the answer contrary to the "closers". Looking through the Help files did not help me in the slightest. $\endgroup$ – Pixel Oct 2 '14 at 6:00
3
$\begingroup$

Try this:

 Table[x^n, {n, 1, 5}] /. x -> "2"
$\endgroup$
  • $\begingroup$ I like this way indeed - much simpler for me than HoldForm, and I can do things like: Table[x^n*n^y, {n, 1, 5}] /. {x -> "2", y -> "3"} Thanks. $\endgroup$ – Pixel Oct 8 '14 at 15:56
3
$\begingroup$
Table[With[{n = n}, HoldForm[2^n]], {n, 1, 5}]
(* or Table[With[{n = n}, Defer[2^n]], {n, 1, 5}] *)

enter image description here

Table[With[{n = n}, HoldForm@n^2], {n, 1, 5}]
(* or Table[With[{n = n}, Defer@n^2], {n, 1, 5}] *)

enter image description here

Update: If 1 and 1^2 (similarly, 2 and 2^1) are both acceptable, then you can use a simpler variant of Alexey's answer without the need for ReplaceAll:

{Array["2"^# &, {5}], Array[#^"2" &, {5}]} 

enter image description here

or

{Table["2"^n, {n, 1, 5}], Table[n^"2", {n, 1, 5}]}

enter image description here

$\endgroup$
0
$\begingroup$

As Nasser suggested, the following works for the first part of the post:

Table[HoldForm@2^n, {n, 1, 5}]

I found the answer for EDIT1: to be:

Table[(HoldForm[2^#1] &)[i], {i, 1, 5}]
$\endgroup$
  • 1
    $\begingroup$ You might find Table[(Defer[2^#1] &)[i], {i, 1, 5}] sometimes more suitable. If you copy and paste it, it can be evaluated without using ReleaseHold. $\endgroup$ – Michael E2 Oct 1 '14 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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