Sub topic 6
Series You Need for Loop Counting
This chapter assumes you can read Python loops, handle basic algebra with exponents, and know what a logarithm is. It gives you two formulas for later chapters. Triangular nested loops produce arithmetic series. Loops that halve or double produce geometric series. Here you learn to sum both kinds quickly and to check your answer in code.
Arithmetic Series
An arithmetic series is a sum whose terms change by the same amount each time. The most common one in loop counting is the sum of the first n whole numbers:
1 + 2 + 3 + ... + n = n(n + 1) / 2
To see why, write the sum forward and backward and add the two rows term by term. Each pair adds up to n + 1, and there are n pairs. That gives n(n + 1) for twice the sum.
The general form works for any arithmetic sequence:
sum = (number of terms) × (first + last) / 2
Watch the endpoints
Most mistakes here are off-by-one errors. Loops in Python usually count from 0 to n − 1, so the sum you meet is often:
0 + 1 + ... + (n − 1) = n(n − 1) / 2
| Series | Terms | Sum |
|---|---|---|
| 1 + 2 + ... + n | n | n(n + 1)/2 |
| 0 + 1 + ... + (n − 1) | n | n(n − 1)/2 |
| a, a + d, ..., L | (L − a)/d + 1 | terms × (a + L)/2 |
Both of the first two sums grow like n²/2. That is the reason triangular loops end up quadratic.
def arithmetic_sum(first, last, step):
count = (last - first) // step + 1
return count * (first + last) // 2
n = 100
assert arithmetic_sum(1, n, 1) == sum(range(1, n + 1)) == n * (n + 1) // 2
assert arithmetic_sum(0, n - 1, 1) == sum(range(n)) == n * (n - 1) // 2
print(arithmetic_sum(3, 99, 4)) # 1275Geometric Series
A geometric series is a sum whose terms are multiplied by the same ratio r each time. For r ≠ 1:
1 + r + r² + ... + r^k = (r^(k+1) − 1) / (r − 1)
For loop counting, the most useful case is r = 2:
1 + 2 + 4 + ... + 2^k = 2^(k+1) − 1
The total is just under twice the largest term. When a series grows geometrically, the last term accounts for most of the sum.
Halving sums stay linear
The same idea run in reverse covers loops that do work proportional to a value they keep halving:
n + n/2 + n/4 + ... + 1 < 2n
A common error is to reason "about log n terms, each up to n, so n log n." That gives an upper bound, but a loose one. The terms shrink so fast that the total is below 2n, which is linear.
def halving_total(n):
total = 0
while n > 0:
total += n
n //= 2
return total
for k in range(10):
assert sum(2**i for i in range(k + 1)) == 2**(k + 1) - 1
print(halving_total(1000)) # 1994, below 2 * 1000Common Errors
- Counting terms wrong. The sequence a, a + d, ..., L has (L − a)/d + 1 terms, not (L − a)/d.
- Mixing up endpoints. Check whether the sum starts at 0 or 1 and whether it stops at n or n − 1.
- Overestimating geometric sums. Multiplying the number of terms by the largest term gives a bound that is too loose. Use the closed form.
Exercise
Find the exact value of 3 + 7 + 11 + ... + 99.
Show answer
The step is 4, so there are (99 − 3)/4 + 1 = 25 terms. The sum is 25 × (3 + 99)/2 = 1275.
Exercise
A loop does 1, 3, 9, ..., 3^k units of work across its iterations. What is the total, and roughly how does it compare to the last term?
Show answer
The total is (3^(k+1) − 1)/2, which is about 1.5 × 3^k. The last term accounts for most of the sum.
Exercise
Someone claims that n + n/2 + n/4 + ... + 1 is Theta(n log n). Are they right?
Show answer
No. The geometric series is less than 2n, so the sum is Theta(n). The n log n figure comes from treating all log n terms as if each were as large as n.