Arithmetic Progressions
An arithmetic progression is a sequence where you add the same number every step: 3, 7, 11, 15, … This lesson gives the formula for the -th term, the famous trick for adding up the terms, and shows where arithmetic progressions hide in code: loop counters, memory addresses and the cost of nested loops.
The common difference
In an arithmetic progression (AP, also called an arithmetic sequence) each term is the previous one plus a fixed number , the common difference:
The term number is one more than the number of steps taken, which is why the formula has and not .
| First terms | |||
|---|---|---|---|
| 3, 7, 11, 15, … | 3 | 4 | |
| 20, 17, 14, 11, … | 20 | −3 | |
| 0.5, 1, 1.5, 2, … | 0.5 | 0.5 |
To test whether a list is an AP, subtract each term from the next. If all the differences are equal, it is one. Plotted against , an AP lies on a straight line with slope : an AP is a linear function sampled at whole numbers (see Functions).
Tip: Two terms are enough to pin down an AP. If and , then five steps added 20, so and .
Adding up the terms
The sum of the first terms is written . Write the sum forwards and backwards and add the two lines: every column adds to the same value, .
In words: the sum is the number of terms times the average of the first and last term. The special case is used constantly in computer science.
def ap_sum(a1, d, n):
an = a1 + (n - 1) * d
return n * (a1 + an) // 2 # n·(a1 + an) is always even for whole-number a1, d
# 3 + 7 + 11 + ... (20 terms)
print("formula:", ap_sum(3, 4, 20))
print("loop :", sum(3 + 4 * k for k in range(20)))
# 1 + 2 + ... + 100
n = 100
print("1..100 :", n * (n + 1) // 2, sum(range(1, n + 1)))formula: 820
loop : 820
1..100 : 5050 5050
In software
Loops and memory. range(start, stop, step) is an arithmetic progression. So is the list of memory addresses of an array: element of an array of 8-byte numbers starting at address base lives at base + 8·i, an AP with . That is why array indexing is a single multiply-and-add (see Arrays).
Nested loops. Many algorithms run an inner loop whose length grows by one each time. Bubble sort and selection sort compare pairs like this:
for i in range(n):
for j in range(i + 1, n): # n − 1 − i steps
...The inner loop runs times, which is the AP sum . For large that is about , so the algorithm is . This is exactly the analysis in Time Complexity and Simple Sorts.
def count_pairs(n):
steps = 0
for i in range(n):
for j in range(i + 1, n):
steps += 1
return steps
for n in [10, 100, 1000]:
print(f"n = {n:>4}: loop counted {count_pairs(n):>6}, formula n(n-1)/2 = {n * (n - 1) // 2:>6}")n = 10: loop counted 45, formula n(n-1)/2 = 45
n = 100: loop counted 4950, formula n(n-1)/2 = 4950
n = 1000: loop counted 499500, formula n(n-1)/2 = 499500
Growing an array by a constant. If a dynamic array grows by a fixed 10 slots every time it fills up, the capacities form an AP (10, 20, 30, …), and the total copying work up to elements is an AP sum, about . That quadratic cost is why real dynamic arrays grow by a factor instead, a geometric progression (next lesson).
Simple interest. A balance that earns the same fixed amount each year (simple interest) grows arithmetically: 1000, 1050, 1100, … at 5% of the original 1000.
Exercises
Exercise 1: The 40th term
Find for the AP 12, 9, 6, 3, …
Answer
Solution
Exercise 2: From two terms
An AP has and . Find , and .
Answer
, , .
Solution
Exercise 3: Multiples of 7
What is the sum of all multiples of 7 between 1 and 500? Check it in code.
Answer
17 892
Solution
print(sum(range(7, 501, 7)))
print(71 * (7 + 497) // 2)17892
17892
Exercise 4: Triangle of loops
A loop runs for i in range(1, n + 1): for j in range(i): work(). How many times does work() run for ? Which growth class is it?
Answer
1275 times; .
Solution
Test yourself
What is the common difference of 8, 5, 2, −1, …?
- 3
- 5
What is ?
- 40000
- 20000
- 20100
- 20200
An inner loop runs times as the outer loop advances. The total is about…