Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
share|improve this question
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 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. –  Oh my ghost Jan 14 at 16:03
1  
I need 24 hours to decide the accepted answer. –  Oh my ghost Jan 14 at 16:17

5 Answers 5

up vote 11 down vote accepted

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

share|improve this answer
    
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 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 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 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.

share|improve this answer
    
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 at 16:12
    
@Szabolcs Strange; yes, I am. So the solution is to put TeXForm inside? –  Mr.Wizard Jan 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 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 at 16:19

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

share|improve this answer
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

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

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}

share|improve this answer
    
+1 but $i^1$ should be replaced with $i$. –  Oh my ghost Jan 15 at 6:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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