6
$\begingroup$

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
$\endgroup$
2
  • 1
    $\begingroup$ Do you want to generate LaTeX code or do you just want to format this on screen? The latter is easier. $\endgroup$
    – Szabolcs
    Jan 14 '14 at 16:02
  • $\begingroup$ @Szabolcs: First priority is to produce the LaTeX output, but it will be nice if the on-screen output is also provided. $\endgroup$ Jan 14 '14 at 16:03
14
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Jan 14 '14 at 16:22
  • $\begingroup$ I realize it's not a matter of With, so I wonder, why use e = i^k rather than e = k and i^e? $\endgroup$
    – Mr.Wizard
    Jan 14 '14 at 16:27
  • $\begingroup$ Gah, can't read today. Now I see why you did that, and +1. However, consider wrapping everything in Block[{i, n}, ...] IMHO. $\endgroup$
    – Mr.Wizard
    Jan 14 '14 at 16:32
13
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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 .. $\endgroup$
    – Szabolcs
    Jan 14 '14 at 16:12
  • $\begingroup$ @Szabolcs Strange; yes, I am. So the solution is to put TeXForm inside? $\endgroup$
    – Mr.Wizard
    Jan 14 '14 at 16:14
  • $\begingroup$ @Szabolcs No, that doesn't work either. Do you mind if I copy your method to my answer? $\endgroup$
    – Mr.Wizard
    Jan 14 '14 at 16:15
  • $\begingroup$ Okay, go ahead. There seems to be a bug in v9 with Row and TeXForm. Can't use them together. $\endgroup$
    – Szabolcs
    Jan 14 '14 at 16:19
8
$\begingroup$
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

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

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

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

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}

$\endgroup$
0

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.