Most programmers (including me) are painfully aware of quadratic behavior resulting from a loop that internally performs 1, 2, 3, 4, 5 and so on operations per iteration,
n∑i=1i=n(n+1)2
It’s very easy to derive, e.g. considering the double of the sum like (1+2+3)+(3+2+1)=3×4 .
For the sum of squares there is a very similar formula,
n∑i=1i2=n(n+12)(n+1)3
But the only way I found to derive that was to assume it would sum to a cubic polynomial f(x)=Ax3+Bx2+Cx+D, and solve for f(n)−f(n−1)=n2.
I’m guessing that there is a much simpler system and concept here, generalizing to ∑ni=1ik?
No comments:
Post a Comment