Series
A series is the sum of the terms of a sequence. This lesson introduces sigma notation (the compact 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 , mathematicians write
The variable is the index of summation, 1 and 10 are the lower and upper limits, and is the general term. In one line we write . The index is a dummy name: and mean the same thing, just like renaming a loop variable.
A finite sum is exactly a for loop with an accumulator:
# 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 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 :
| Rule | Formula |
|---|---|
| constant term | |
| pull out a factor | |
| split a sum | |
| split the range | |
| shift the index |
Note: There is no rule for products: is usually not . Try it with and : , but .
Standard sums
With the rules and a few known sums you can add up any polynomial in .
| Sum | Closed form | |
|---|---|---|
| 10 | ||
| 55 | ||
| 385 | ||
| 3025 | ||
| (geometric) |
For example, . Simplified, that is . You can prove formulas like these with mathematical induction.
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 :
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 , which works out to , the number of ways to choose 3 items from : . The leading term of the closed form is the growth class you see in Time Complexity.
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 , 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 .
Answer
Solution
Exercise 2: Use the table
Find .
Answer
3710
Solution
print(sum(k**2 + 4 * k for k in range(1, 21)))3710
Exercise 3: Odd numbers
Write in sigma notation and find a closed form.
Answer
Solution
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 .
Answer
, which is 5050 for .
Solution
n = 100
print(sum(1 for i in range(1, n + 1) for j in range(i, n + 1)))5050
Test yourself
What is ?
- 64
- 100
- 30
- 10
Which statement is true for all sequences?
Two nested loops where the inner one runs times for do how much work?