# How to make a table of $\sum_{i=1}^n$?

I want to create a table of $\displaystyle \sum_{i=1}^n i^k$ for $1\le k\le 5$. The output I want to obtain is a TeXForm output as follows.

\begin{align} \sum_{i=1}^n i &=\frac1 2 n(n-1)\\ \sum_{i=1}^n i^2 &=\frac1 2 n(n-1)(2n+1)\\ &\vdots \end{align}

My attempt is as follows but it does not produce what I want to achieve.

Sum[i^k, {i, 1, n}] // Table[Factor[#], {k, 1, 5}] & // TableForm

• Do you want to generate LaTeX code or do you just want to format this on screen? The latter is easier. Jan 14 '14 at 16:02
• @Szabolcs: First priority is to produce the LaTeX output, but it will be nice if the on-screen output is also provided. Jan 14 '14 at 16:03

Here's a way that I find clear:

Make sure i and n have no value: i=.; n=..

Generate a list of the held sum expressions, taking care to make the exponent go away from $i^1$.

list = Table[With[{e = i^k}, HoldForm[Sum[e, {i, 1, n}]]], {k, 5}]


Use ReleaseHold to make a table of results:

TeXForm@TableForm[# == ReleaseHold[#] & /@ list]


$$\begin{array}{c} \sum _{i=1}^n i=\frac{1}{2} n (n+1) \\ \sum _{i=1}^n i^2=\frac{1}{6} n (n+1) (2 n+1) \\ \sum _{i=1}^n i^3=\frac{1}{4} n^2 (n+1)^2 \\ \sum _{i=1}^n i^4=\frac{1}{30} n (n+1) (2 n+1) \left(3 n^2+3 n-1\right) \\ \sum _{i=1}^n i^5=\frac{1}{12} n^2 (n+1)^2 \left(2 n^2+2 n-1\right) \\ \end{array}$$

• This method (using With), as written, has a disadvantage in that a global value of i will be substituted if it exists. The same is true of n in both our methods but I cleared n beforehand to address that. Jan 14 '14 at 16:22
• I realize it's not a matter of With, so I wonder, why use e = i^k rather than e = k and i^e? Jan 14 '14 at 16:27
• Gah, can't read today. Now I see why you did that, and +1. However, consider wrapping everything in Block[{i, n}, ...] IMHO. Jan 14 '14 at 16:32

Here is a start (adjusted based on Szabolcs's statement that Row and TeXForm don't work together in v9):

Clear[n]

# == ReleaseHold[#] & /@
Array[HoldForm[Sum[i^#, {i, 1, n}]] &, 5] // Column // TeXForm


$\begin{array}{l} \sum _{i=1}^n i^1=\frac{1}{2} n (n+1) \\ \sum _{i=1}^n i^2=\frac{1}{6} n (n+1) (2 n+1) \\ \sum _{i=1}^n i^3=\frac{1}{4} n^2 (n+1)^2 \\ \sum _{i=1}^n i^4=\frac{1}{30} n (n+1) (2 n+1) \left(3 n^2+3 n-1\right) \\ \sum _{i=1}^n i^5=\frac{1}{12} n^2 (n+1)^2 \left(2 n^2+2 n-1\right) \end{array}$

The two Function expressions could have been combined, but I felt that this was easier to read and easier to adjust.

Edit: I note that Szabolcs's answer shows the values above and below the Sigma symbols. Apparently that is a version difference, as I am using version 7 and his code produces the same output as mine in this regard.

• Are you using version 7? When I try to apply TeXForm to any kind of Row expression in v9, I get an error "TeXForm of \!$$TemplateSlotSequence[1, \"\"]$$ is not supported." I thought it must have worked before .. Jan 14 '14 at 16:12
• @Szabolcs Strange; yes, I am. So the solution is to put TeXForm inside? Jan 14 '14 at 16:14
• @Szabolcs No, that doesn't work either. Do you mind if I copy your method to my answer? Jan 14 '14 at 16:15
• Okay, go ahead. There seems to be a bug in v9 with Row and TeXForm. Can't use them together. Jan 14 '14 at 16:19
TableForm[Table[{
HoldForm[Sum[i^z, {i, 1, n}]] /. z -> k, "=",
Factor[Sum[i^k, {i, 1, n}]]},
{k, 1, 5}]]


• +1. Won't adding // TeXForm at the end make it TeXForm? Jan 14 '14 at 16:19
• @RunnyKine - not on version 7 Jan 14 '14 at 16:40
• Ah, I see. It works on Version 9. Jan 14 '14 at 17:00

In Version 10, we can use Inactivate and Activate to achieve this easily:

With[{rl = Array[{m -> #} &, 5], s = Inactivate[Sum[i^m, {i, 1, n}], Sum]},
Thread[Equal[s /. rl, Factor[Activate[s] /. rl]]]] // Column // TeXForm


\begin{array}{l} \underset{i=1}{\overset{n}{\sum }}i=\frac{1}{2} n (n+1) \\ \underset{i=1}{\overset{n}{\sum }}i^2=\frac{1}{6} n (n+1) (2 n+1) \\ \underset{i=1}{\overset{n}{\sum }}i^3=\frac{1}{4} n^2 (n+1)^2 \\ \underset{i=1}{\overset{n}{\sum }}i^4=\frac{1}{30} n (n+1) (2 n+1) \left(3 n^2+3 n-1\right) \\ \underset{i=1}{\overset{n}{\sum }}i^5=\frac{1}{12} n^2 (n+1)^2 \left(2 n^2+2 n-1\right) \\ \end{array}

Note that this also does not give the undesired i^1

• The generated output \underset{i=1}{\overset{n}{\sum }} is too complicated. Jan 18 '18 at 4:15

Another entry:

With[{rule = Array[{m -> #} &, 5], sm = HoldForm@Sum[i^m, {i, 1, n}]},
Thread[Equal[sm /. rule, Factor[ReleaseHold@sm /. rule]]]] // Column // TeXForm


\begin{array}{l} \sum _{i=1}^n i^1=\frac{1}{2} n (n+1) \\ \sum _{i=1}^n i^2=\frac{1}{6} n (n+1) (2 n+1) \\ \sum _{i=1}^n i^3=\frac{1}{4} n^2 (n+1)^2 \\ \sum _{i=1}^n i^4=\frac{1}{30} n (n+1) (2 n+1) \left(3 n^2+3 n-1\right) \\ \sum _{i=1}^n i^5=\frac{1}{12} n^2 (n+1)^2 \left(2 n^2+2 n-1\right) \\ \end{array}

Or to get rid of the 1 in i^1

With[{hd = Join[{HoldForm@Sum[i, {i, 1, n}]}, HoldForm@Sum[i^m, {i, 1, n}] /. Table[{m -> j},
{j, 2, 5}]], sm = Factor[Sum[i^m, {i, 1, n}] /. Array[{m -> #} &, 5]]},
Thread[Equal[hd, sm]]] // Column // TeXForm


\begin{array}{l} \sum _{i=1}^n i=\frac{1}{2} n (n+1) \\ \sum _{i=1}^n i^2=\frac{1}{6} n (n+1) (2 n+1) \\ \sum _{i=1}^n i^3=\frac{1}{4} n^2 (n+1)^2 \\ \sum _{i=1}^n i^4=\frac{1}{30} n (n+1) (2 n+1) \left(3 n^2+3 n-1\right) \\ \sum _{i=1}^n i^5=\frac{1}{12} n^2 (n+1)^2 \left(2 n^2+2 n-1\right) \\ \end{array}