2

I have a loop that iterates a variable $i$ from 0 to some boundary $b$. Let $p=4i+4$. Each iteration of the loop I add $(p^3+5p)/3+2$ to a sum variable $s$ which is initially set to 0.

Question: Instead of this loop, how can I convert this all into a closed form expression that gives me the same $s$ based on the value of $b$?

1 Answers1

3

In the end of the loop, if I understand correctly, you have: $$\begin{align*}s&=\sum_{i=0}^b \left(\frac{(4i+4)^3+5(4i+4)}{3}+2\right)=\sum_{i=0}^b \left(4\frac{16i^3+48i^2+53i+21}{3}+2\right)\\&=\frac{64}{3}\sum_{i=0}^b i^3+64\sum_{i=0}^b i^2+\frac{212}{3}\sum_{i=0}^b i+30\sum_{i=0}^b 1 \end{align*}$$ Using the formulas (which you can prove by induction, if you want): $$\sum_{i=0}^b 1=b+1, \hspace{10pt} \sum_{i=0}^b i=\frac{b(b+1)}{2}, \hspace{10pt} \sum_{i=0}^b i^2=\frac{b(b+1)(2b+1)}{6}, \hspace{10pt} \sum_{i=0}^b i^3=\frac{b^2(b+1)^2}{4}$$ You can get: $$\begin{align*}s&=\frac{64}{3}\frac{b^2(b+1)^2}{4}+64\frac{b(b+1)(2b+1)}{6}+\frac{212}{3}\frac{b(b+1)}{2}+30(b+1)\\&=\frac{16}3 b^4+32 b^3+\frac{218}3 b^2+76 b+30=\frac23(b+1)(8 b^3+40 b^2+69 b+45)\end{align*}$$

Dennis Gulko
  • 15,640
  • This is not technically a closed form expression. Closed form means the only variable involved should be $b$, without loops. $i$ is an iterator variable that only makes sense in the context of a loop. – MyNameIsKhan Nov 26 '12 at 23:36
  • How is that not a closed form? – Dennis Gulko Nov 26 '12 at 23:42
  • I made that comment before your final edit; checking it now – MyNameIsKhan Nov 26 '12 at 23:43
  • Is there any way to generalize this to arbitrary p (maybe in another equation p=5i+6 instead, etc)? I'll accept this as answer anyway in the meantime, just curious – MyNameIsKhan Nov 26 '12 at 23:47
  • As you can see, you can expand the way i did in the beginning until you can use the formulas I mentioned. Try to do what I did for $p=5i+6$ (just open the parenthesis). There is no 'magic' formula where you can just plug-in $p$ and have an answer without some computations. – Dennis Gulko Nov 26 '12 at 23:53
  • so with p=4i+1, is the sum (64(b(b+1)/2)*2 + 48(b(b+1)(2b+1)/6) + 32(b(b+1)/2) + 22b)/3 + 3*b? Getting the wrong answer despite following this method – MyNameIsKhan Nov 27 '12 at 00:00
  • Ah I gotcha -- it's (64(b(b+1)/2)*2 + 48(b(b+1)(2b+1)/6) + 32(b(b+1)/2) + 6(b+1))/3 + 2*(b+1). I understand now. Thank you! – MyNameIsKhan Nov 27 '12 at 00:10
  • If $p=4i+1$, in the sum you will have $\frac13((4i+1)^3+5(4i+1))+2=\frac13(64i^3+48i^2+32i+6)+2=\frac13(64i^3+48i^2+32i+12)$, so your coefficients seem to be wrong. There is a $\frac13$ missing... – Dennis Gulko Nov 27 '12 at 00:12