6

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
kiss my armpit
  • 757
  • 4
  • 15
  • 1
    Do you want to generate LaTeX code or do you just want to format this on screen? The latter is easier. – Szabolcs 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. – kiss my armpit Jan 14 '14 at 16:03

5 Answers5

14

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} $$

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 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. – Mr.Wizard 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? – Mr.Wizard 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. – Mr.Wizard Jan 14 '14 at 16:32
13

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 .. – Szabolcs Jan 14 '14 at 16:12
  • @Szabolcs Strange; yes, I am. So the solution is to put TeXForm inside? – Mr.Wizard Jan 14 '14 at 16:14
  • @Szabolcs No, that doesn't work either. Do you mind if I copy your method to my answer? – Mr.Wizard 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. – Szabolcs Jan 14 '14 at 16:19
8
TableForm[Table[{
   HoldForm[Sum[i^z, {i, 1, n}]] /. z -> k, "=",
   Factor[Sum[i^k, {i, 1, n}]]},
  {k, 1, 5}]]

enter image description here

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
8

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

RunnyKine
  • 33,088
  • 3
  • 109
  • 176
5

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}

RunnyKine
  • 33,088
  • 3
  • 109
  • 176