# Converting loop to a closed form expression? [duplicate]

Possible Duplicate:
How to convert this loop into a closed form expression?

I have the following code in Python

    total=0
for x in range(lowerBound,upperBound+1,2):
total+=(x*x*x+5*x)/3+2


This iterates from x=lowerBound to the maximum value of x=upperBound (going by intervals of 2 at a time), tossing the value of $\lfloor(x^3+5x)/3\rfloor+2$ into the total (all division is floor division in Python unless using decimals, which I'd like to avoid if possible for precision's sake).

How can I convert this to a closed form expression?

-

## marked as duplicate by joriki, Martin Argerami, copper.hat, mixedmath♦Nov 26 '12 at 6:46

What you want is, for $L$ and $U$ gotten from your lower and upper bounds, $S = \sum_{x=L}^U \lfloor((2x)^3+5(2x))/3\rfloor+2$.
mod 3, $x^3+5x = x^3-x = x(x-1)(x+1)$, and one of these is always divisible by 3, so we can ignore the floor. You can also argue that $x^3+5x = 0, 6,$ and $18$ for $x = 0, 1,$ and $2$.
The $2$ contributes $2(U-L+1)$, so it will also be ignored.
We are left with $T = \sum_{x=L}^U ((2x)^3+5(2x))/3 = (1/3)\sum_{x=L}^U 8x^3+10x$.