TExcelTAD
Home › Discrete Mathematics › Sums and Closed Forms

Sums and Closed Forms

Analysing an algorithm usually ends in a sum: the work of a loop is the sum of the work of its iterations. A closed form replaces the sum by a formula you can evaluate at once, such as ∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}. This lesson collects the closed forms every programmer should know and three methods to find new ones: the perturbation method, guess-and-verify, and derivatives of known sums.

The essential closed forms

Sum Closed form
∑i=1n1\sum_{i=1}^{n} 1 nn
∑i=1ni\sum_{i=1}^{n} i n(n+1)2\frac{n(n+1)}{2}
∑i=1ni2\sum_{i=1}^{n} i^2 n(n+1)(2n+1)6\frac{n(n+1)(2n+1)}{6}
∑i=1ni3\sum_{i=1}^{n} i^3 (n(n+1)2)2\left(\frac{n(n+1)}{2}\right)^2
∑i=0n−1xi\sum_{i=0}^{n-1} x^i (geometric, x≠1x \ne 1) 1−xn1−x\frac{1 - x^n}{1 - x}
∑i=0∞xi\sum_{i=0}^{\infty} x^i (∣x∣<1\lvert x \rvert < 1) 11−x\frac{1}{1 - x}
∑i=1nixi\sum_{i=1}^{n} i x^i (x≠1x \ne 1) x−(n+1)xn+1+nxn+2(1−x)2\frac{x - (n+1)x^{n+1} + nx^{n+2}}{(1 - x)^2}

Each is proved by induction once you know it. Finding it is the creative part.

The perturbation method

Let S=1+x+x2+⋯+xn−1S = 1 + x + x^2 + \cdots + x^{n-1}. Multiply by xx ("perturb" the sum): xS=x+x2+⋯+xnxS = x + x^2 + \cdots + x^n. Subtracting, almost everything cancels:

S−xS=1−xn⇒S=1−xn1−xS - xS = 1 - x^n \quad\Rightarrow\quad S = \frac{1 - x^n}{1 - x}

The same idea works for ∑ixi\sum i x^i, and for the money question: a loan of LL repaid by nn yearly payments pp at interest rate rr. Paying pp in kk years is worth p(1+r)k\frac{p}{(1 + r)^k} today, so

L=∑k=1np(1+r)k=p⋅1−(1+r)−nrL = \sum_{k=1}^{n} \frac{p}{(1+r)^k} = p \cdot \frac{1 - (1+r)^{-n}}{r}
python · try it yourself
from fractions import Fraction

def geometric(x, n):
    return (1 - x ** n) / (1 - x)

x, n = Fraction(1, 3), 10
print("direct sum:", sum(x ** i for i in range(n)), " closed form:", geometric(x, n))

# loan: how much can you borrow with 10 yearly payments of 1000 at 5% interest?
r, p, n = 0.05, 1000, 10
print("by summing:", round(sum(p / (1 + r) ** k for k in range(1, n + 1)), 2))
print("closed form:", round(p * (1 - (1 + r) ** -n) / r, 2))
direct sum: 29524/19683  closed form: 29524/19683
by summing: 7721.73
closed form: 7721.73

Guess and verify

When a sum looks polynomial, guess its degree and fit coefficients. ∑i=1ni2\sum_{i=1}^n i^2 grows like ∫0nx2 dx=n33\int_0^n x^2\,dx = \frac{n^3}{3}, so guess a cubic an3+bn2+cn+dan^3 + bn^2 + cn + d, fit it to n=0,1,2,3n = 0, 1, 2, 3, then prove the result by induction. SymPy automates both steps:

python · try it yourself
from sympy import symbols, summation, factor, simplify

i, n, x = symbols("i n x", positive=True, integer=True)
print("sum i     =", factor(summation(i, (i, 1, n))))
print("sum i^2   =", factor(summation(i ** 2, (i, 1, n))))
print("sum i^3   =", factor(summation(i ** 3, (i, 1, n))))
print("sum i*2^i =", simplify(summation(i * 2 ** i, (i, 1, n))))
sum i     = n*(n + 1)/2
sum i^2   = n*(n + 1)*(2*n + 1)/6
sum i^3   = n**2*(n + 1)**2/4
sum i*2^i = 2*2**n*n - 2*2**n + 2

The last closed form, ∑i=1ni 2i=(n−1)2n+1+2\sum_{i=1}^{n} i \, 2^i = (n - 1)2^{n+1} + 2, appears in the analysis of building a binary heap.

Derivatives of sums

Differentiating a known sum gives new ones. From ∑i=0∞xi=11−x\sum_{i=0}^{\infty} x^i = \frac{1}{1-x}, differentiate and multiply by xx:

∑i=0∞ixi=x(1−x)2,so∑i=1∞i2i=1/2(1/2)2=2\sum_{i=0}^{\infty} i x^i = \frac{x}{(1 - x)^2}, \quad\text{so}\quad \sum_{i=1}^{\infty} \frac{i}{2^i} = \frac{1/2}{(1/2)^2} = 2

This sum shows up when computing the expected number of coin flips until the first head (it is 2) and the total work of a heap build (O(n)O(n), not O(nlog⁡n)O(n \log n)).

Double sums

Nested loops give double sums. Swapping the order of summation often helps:

∑i=1n∑j=1i1=∑i=1ni=n(n+1)2\sum_{i=1}^{n} \sum_{j=1}^{i} 1 = \sum_{i=1}^{n} i = \frac{n(n+1)}{2}

which is the number of iterations of for i in range(1, n + 1): for j in range(1, i + 1):. For ∑k=1nHk\sum_{k=1}^{n} H_k with harmonic numbers, swapping gives ∑j=1nn−j+1j=(n+1)Hn−n\sum_{j=1}^{n} \frac{n - j + 1}{j} = (n+1)H_n - n.

In software

Closed forms turn loop costs into Big-O: a triangular double loop does n(n+1)2=Θ(n2)\frac{n(n+1)}{2} = \Theta(n^2) iterations; a loop that doubles its step runs log⁡2n\log_2 n times; a dynamic array that doubles its capacity copies at most 1+2+4+⋯+n<2n1 + 2 + 4 + \cdots + n < 2n elements in total, so appends are O(1)O(1) amortized.

Exercises

Exercise 1: Triangle loop

How many times does the inner statement run? for i in range(n): for j in range(i, n): work()

Answer

n(n+1)2\frac{n(n+1)}{2}.

Solution

For each ii the inner loop runs n−in - i times: ∑i=0n−1(n−i)=n+(n−1)+⋯+1=n(n+1)2\sum_{i=0}^{n-1} (n - i) = n + (n - 1) + \cdots + 1 = \frac{n(n+1)}{2}.

Exercise 2: Geometric sum

Compute ∑i=093i\sum_{i=0}^{9} 3^i with the closed form.

Answer
Solution
∑i=093i=310−13−1=59049−12=29524\sum_{i=0}^{9} 3^i = \frac{3^{10} - 1}{3 - 1} = \frac{59049 - 1}{2} = 29524

Exercise 3: Doubling array (code)

A dynamic array starts with capacity 1 and doubles whenever it is full. Count the element copies needed to append n=1000n = 1000 items and compare with 2n2n.

Answer

1023 copies, less than 2n=20002n = 2000.

Solution

Resizes happen at sizes 1,2,4,…,5121, 2, 4, \ldots, 512, copying 1+2+⋯+512=210−1=10231 + 2 + \cdots + 512 = 2^{10} - 1 = 1023 elements.

python · try it yourself
cap, size, copies = 1, 0, 0
for _ in range(1000):
    if size == cap:
        copies += size
        cap *= 2
    size += 1
print(copies, 2 * 1000)
1023 2000

Test yourself

What is ∑i=1100i\sum_{i=1}^{100} i?

  1. 5000
  2. 5050
  3. 10000
  4. 10100

What is ∑i=0∞(1/2)i\sum_{i=0}^{\infty} (1/2)^i?

  1. 1
  2. 1.5
  3. 2
  4. infinite

The perturbation method for S=∑xiS = \sum x^i computes…

  1. S+xS + x
  2. S−xSS - xS
  3. S2S^2
  4. dSdx\frac{dS}{dx}