TExcelTAD

Series

A series is the sum of the terms of a sequence. This lesson introduces sigma notation (the compact ∑\sum way to write long sums), the rules that let you split and rearrange sums, the standard formulas for sums of powers, and how those formulas count the work done by loops in an algorithm.

Sigma notation

Instead of writing 12+22+32+⋯+1021^2 + 2^2 + 3^2 + \cdots + 10^2, mathematicians write

∑k=110k2"the sum of k2 for k from 1 to 10"\sum_{k=1}^{10} k^2 \qquad \text{"the sum of } k^2 \text{ for } k \text{ from 1 to 10"}

The variable kk is the index of summation, 1 and 10 are the lower and upper limits, and k2k^2 is the general term. In one line we write ∑k=110k2\sum_{k=1}^{10} k^2. The index is a dummy name: ∑k=110k2\sum_{k=1}^{10} k^2 and ∑i=110i2\sum_{i=1}^{10} i^2 mean the same thing, just like renaming a loop variable.

A finite sum is exactly a for loop with an accumulator:

python · try it yourself
# sum of k^2 for k = 1..10, three ways
total = 0
for k in range(1, 11):
    total += k**2
print(total)
print(sum(k**2 for k in range(1, 11)))

import sympy as sp
k, n = sp.symbols("k n", integer=True, positive=True)
print(sp.summation(k**2, (k, 1, 10)))
print(sp.factor(sp.summation(k**2, (k, 1, n))))
385
385
385
n*(n + 1)*(2*n + 1)/6

SymPy can do the sum with a symbolic upper limit nn and hands back the closed formula. (SymPy prints * for multiplication and ** for powers.)

Rules for sums

Sums behave like loops you can rearrange. For constants cc:

Rule Formula
constant term ∑k=1nc=c⋅n\sum_{k=1}^{n} c = c \cdot n
pull out a factor ∑c⋅ak=c⋅∑ak\sum c \cdot a_k = c \cdot \sum a_k
split a sum ∑(ak+bk)=∑ak+∑bk\sum (a_k + b_k) = \sum a_k + \sum b_k
split the range ∑k=1nak=∑k=1mak+∑k=m+1nak\sum_{k=1}^{n} a_k = \sum_{k=1}^{m} a_k + \sum_{k=m+1}^{n} a_k
shift the index ∑k=1nak=∑j=0n−1aj+1\sum_{k=1}^{n} a_k = \sum_{j=0}^{n-1} a_{j+1}

Note: There is no rule for products: ∑akbk\sum a_k b_k is usually not (∑ak)(∑bk)(\sum a_k)(\sum b_k). Try it with ak=bk=ka_k = b_k = k and n=2n = 2: 1+4=51 + 4 = 5, but 3⋅3=93 \cdot 3 = 9.

Standard sums

With the rules and a few known sums you can add up any polynomial in kk.

Sum Closed form n=10n = 10
∑k=1n1\sum_{k=1}^{n} 1 nn 10
∑k=1nk\sum_{k=1}^{n} k n(n+1)2\frac{n(n + 1)}{2} 55
∑k=1nk2\sum_{k=1}^{n} k^2 n(n+1)(2n+1)6\frac{n(n + 1)(2n + 1)}{6} 385
∑k=1nk3\sum_{k=1}^{n} k^3 (n(n+1)2)2\left(\frac{n(n + 1)}{2}\right)^2 3025
∑k=0n−1rk\sum_{k=0}^{n-1} r^k 1−rn1−r\frac{1 - r^n}{1 - r} (geometric)

For example, ∑k=1n(6k2−2k+3)=6⋅n(n+1)(2n+1)6−2⋅n(n+1)2+3n=n(n+1)(2n+1)−n(n+1)+3n\sum_{k=1}^{n} (6k^2 - 2k + 3) = 6 \cdot \frac{n(n + 1)(2n + 1)}{6} - 2 \cdot \frac{n(n + 1)}{2} + 3n = n(n + 1)(2n + 1) - n(n + 1) + 3n. Simplified, that is 2n3+2n2+3n2n^3 + 2n^2 + 3n. You can prove formulas like these with mathematical induction.

python · try it yourself
def closed(n):
    return 2 * n**3 + 2 * n**2 + 3 * n

for n in [1, 5, 20]:
    loop = sum(6 * k**2 - 2 * k + 3 for k in range(1, n + 1))
    print(n, loop, closed(n))
1 7 7
5 315 315
20 16860 16860

In software: counting loop work

Algorithm analysis is largely the art of turning loops into sums and sums into formulas. Each nested loop is one ∑\sum:

text
for i in range(n):            ∑ᵢ₌₀ⁿ⁻¹
    for j in range(i):            ∑ⱼ₌₀ⁱ⁻¹  1   = i
        work()
total = ∑ᵢ₌₀ⁿ⁻¹ i = n(n − 1)/2         → O(n²)

Add a third loop for k in range(j) and the total becomes ∑i∑jj\sum_i \sum_j j, which works out to n(n−1)(n−2)6\frac{n(n - 1)(n - 2)}{6}, the number of ways to choose 3 items from nn: O(n3)O(n^3). The leading term of the closed form is the growth class you see in Time Complexity.

python · try it yourself
def triple(n):
    count = 0
    for i in range(n):
        for j in range(i):
            for k in range(j):
                count += 1
    return count

for n in [10, 30, 60]:
    print(n, triple(n), n * (n - 1) * (n - 2) // 6)
10 120 120
30 4060 4060
60 34220 34220

Sums also show up as running totals in data code: itertools.accumulate and NumPy's cumsum produce the sequence of partial sums S1,S2,S3,…S_1, S_2, S_3, \ldots, which the next lesson uses to decide whether an infinite sum has a value. The deeper theory is in Series and Sigma Notation.

Exercises

Exercise 1: Expand the sigma

Write out and evaluate ∑k=25(3k−1)\sum_{k=2}^{5} (3k - 1).

Answer

5+8+11+14=385 + 8 + 11 + 14 = 38

Solution
k=2: 5,k=3: 8,k=4: 11,k=5: 145+8+11+14=38Check with rules: 3⋅(2+3+4+5)−4⋅1=3⋅14−4=38\begin{aligned} &k = 2{:}\ 5, \quad k = 3{:}\ 8, \quad k = 4{:}\ 11, \quad k = 5{:}\ 14 \\ &5 + 8 + 11 + 14 = 38 \\ &\text{Check with rules: } 3 \cdot (2 + 3 + 4 + 5) - 4 \cdot 1 = 3 \cdot 14 - 4 = 38 \end{aligned}

Exercise 2: Use the table

Find ∑k=120(k2+4k)\sum_{k=1}^{20} (k^2 + 4k).

Answer

3710

Solution
∑k2=20⋅21⋅416=2870∑4k=4⋅20⋅212=840total=2870+840=3710\begin{aligned} \sum k^2 &= \frac{20 \cdot 21 \cdot 41}{6} = 2870 \\ \sum 4k &= \frac{4 \cdot 20 \cdot 21}{2} = 840 \\ \text{total} &= 2870 + 840 = 3710 \end{aligned}
python · try it yourself
print(sum(k**2 + 4 * k for k in range(1, 21)))
3710

Exercise 3: Odd numbers

Write 1+3+5+⋯+(2n−1)1 + 3 + 5 + \cdots + (2n - 1) in sigma notation and find a closed form.

Answer

∑k=1n(2k−1)=n2\sum_{k=1}^{n} (2k - 1) = n^2

Solution
∑(2k−1)=2⋅n(n+1)2−n=n2+n−n=n2\sum (2k - 1) = 2 \cdot \frac{n(n + 1)}{2} - n = n^2 + n - n = n^2

Exercise 4: Count a loop

How many times does work() run? for i in range(1, n + 1): for j in range(i, n + 1): work(). Give the formula and the value for n=100n = 100.

Answer

n(n+1)2\frac{n(n + 1)}{2}, which is 5050 for n=100n = 100.

Solution
For each i the inner loop runs n−i+1 times:∑i=1n(n−i+1)=n+(n−1)+⋯+1=n(n+1)2\begin{aligned} &\text{For each } i \text{ the inner loop runs } n - i + 1 \text{ times:} \\ &\sum_{i=1}^{n} (n - i + 1) = n + (n - 1) + \cdots + 1 = \frac{n(n + 1)}{2} \end{aligned}
python · try it yourself
n = 100
print(sum(1 for i in range(1, n + 1) for j in range(i, n + 1)))
5050

Test yourself

What is ∑k=14k3\sum_{k=1}^{4} k^3?

  1. 64
  2. 100
  3. 30
  4. 10

Which statement is true for all sequences?

  1. ∑akbk=(∑ak)(∑bk)\sum a_k b_k = (\sum a_k)(\sum b_k)
  2. ∑(ak)2=(∑ak)2\sum (a_k)^2 = (\sum a_k)^2
  3. ∑5ak=5⋅∑ak\sum 5a_k = 5 \cdot \sum a_k
  4. ∑1/ak=1/∑ak\sum 1/a_k = 1/\sum a_k

Two nested loops where the inner one runs ii times for i=0,…,n−1i = 0, \ldots, n - 1 do how much work?

  1. n(n−1)2\frac{n(n - 1)}{2}
  2. n2n^2
  3. 2n2n
  4. nlog⁡nn \log n