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

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

Posted 2014-01-14T15:56:32.120

Reputation: 747

1Do you want to generate LaTeX code or do you just want to format this on screen? The latter is easier. – Szabolcs – 2014-01-14T16:02:17.157

@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 – 2014-01-14T16:03:44.930

Answers

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

Posted 2014-01-14T15:56:32.120

Reputation: 213 047

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 – 2014-01-14T16:22:35.727

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 – 2014-01-14T16:27:52.227

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 – 2014-01-14T16:32:14.787

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

Posted 2014-01-14T15:56:32.120

Reputation: 259 163

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 – 2014-01-14T16:12:50.153

@Szabolcs Strange; yes, I am. So the solution is to put TeXForm inside? – Mr.Wizard – 2014-01-14T16:14:00.033

@Szabolcs No, that doesn't work either. Do you mind if I copy your method to my answer? – Mr.Wizard – 2014-01-14T16:15:52.260

Okay, go ahead. There seems to be a bug in v9 with Row and TeXForm. Can't use them together. – Szabolcs – 2014-01-14T16:19:17.743

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

Posted 2014-01-14T15:56:32.120

Reputation: 27 033

1+1. Won't adding // TeXForm at the end make it TeXForm? – RunnyKine – 2014-01-14T16:19:56.790

1@RunnyKine - not on version 7 – Chris Degnen – 2014-01-14T16:40:48.917

1Ah, I see. It works on Version 9. – RunnyKine – 2014-01-14T17:00:49.463

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

Posted 2014-01-14T15:56:32.120

Reputation: 32 260

The generated output \underset{i=1}{\overset{n}{\sum }} is too complicated. – Money Oriented Programmer – 2018-01-18T04:15:23.597

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

Posted 2014-01-14T15:56:32.120

Reputation: 32 260