TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Arithmetic Progressions

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 nn-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 dd, the common difference:

a1, a1+d, a1+2d, a1+3d, …n-th term:an=a1+(n−1)⋅d\begin{aligned} &a_1,\ a_1 + d,\ a_1 + 2d,\ a_1 + 3d,\ \ldots \\ &n\text{-th term:}\quad a_n = a_1 + (n - 1) \cdot d \end{aligned}

The term number nn is one more than the number of steps taken, which is why the formula has (n−1)(n - 1) and not nn.

First terms a1a_1 dd ana_n
3, 7, 11, 15, … 3 4 4n−14n - 1
20, 17, 14, 11, … 20 −3 23−3n23 - 3n
0.5, 1, 1.5, 2, … 0.5 0.5 0.5n0.5n

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 nn, an AP lies on a straight line with slope dd: an AP is a linear function sampled at whole numbers (see Functions).

Tip: Two terms are enough to pin down an AP. If a4=10a_4 = 10 and a9=30a_9 = 30, then five steps added 20, so d=4d = 4 and a1=10−3⋅4=−2a_1 = 10 - 3 \cdot 4 = -2.

Adding up the terms

The sum of the first nn terms is written SnS_n. Write the sum forwards and backwards and add the two lines: every column adds to the same value, first+last\text{first} + \text{last}.

Sn=a1+(a1+d)+⋯+anSn=an+(an−d)+⋯+a12Sn=(a1+an)+(a1+an)+⋯+(a1+an)←n columnsSn=n⋅(a1+an)2=n2⋅(2a1+(n−1)d)\begin{aligned} S_n &= a_1 + (a_1 + d) + \cdots + a_n \\ S_n &= a_n + (a_n - d) + \cdots + a_1 \\ 2S_n &= (a_1 + a_n) + (a_1 + a_n) + \cdots + (a_1 + a_n) && \leftarrow n \text{ columns} \\[1ex] S_n &= \frac{n \cdot (a_1 + a_n)}{2} = \frac{n}{2} \cdot (2a_1 + (n - 1)d) \end{aligned}

In words: the sum is the number of terms times the average of the first and last term. The special case 1+2+⋯+n=n(n+1)21 + 2 + \cdots + n = \frac{n(n + 1)}{2} is used constantly in computer science.

python · try it yourself
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 ii of an array of 8-byte numbers starting at address base lives at base + 8·i, an AP with d=8d = 8. 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:

python
for i in range(n):
    for j in range(i + 1, n):   # n − 1 − i steps
        ...

The inner loop runs (n−1)+(n−2)+⋯+1+0(n - 1) + (n - 2) + \cdots + 1 + 0 times, which is the AP sum n(n−1)2\frac{n(n - 1)}{2}. For large nn that is about n2/2n^2/2, so the algorithm is O(n2)O(n^2). This is exactly the analysis in Time Complexity and Simple Sorts.

python · try it yourself
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 nn elements is an AP sum, about n2/20n^2/20. 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 a40a_{40} for the AP 12, 9, 6, 3, …

Answer

a40=−105a_{40} = -105

Solution
a1=12, d=−3a40=12+39⋅(−3)=12−117=−105\begin{aligned} &a_1 = 12,\ d = -3 \\ &a_{40} = 12 + 39 \cdot (-3) = 12 - 117 = -105 \end{aligned}

Exercise 2: From two terms

An AP has a5=17a_5 = 17 and a12=45a_{12} = 45. Find dd, a1a_1 and S12S_{12}.

Answer

d=4d = 4, a1=1a_1 = 1, S12=276S_{12} = 276.

Solution
7 steps added 45−17=28⇒d=4a1=17−4⋅4=1S12=12⋅(1+45)2=6⋅46=276\begin{aligned} &\text{7 steps added } 45 - 17 = 28 \quad\Rightarrow\quad d = 4 \\ &a_1 = 17 - 4 \cdot 4 = 1 \\ &S_{12} = \frac{12 \cdot (1 + 45)}{2} = 6 \cdot 46 = 276 \end{aligned}

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
Multiples: 7,14,…,497(497=7⋅71, so n=71 terms)S=71⋅(7+497)2=71⋅252=17892\begin{aligned} &\text{Multiples: } 7, 14, \ldots, 497 && (497 = 7 \cdot 71, \text{ so } n = 71 \text{ terms}) \\ &S = \frac{71 \cdot (7 + 497)}{2} = 71 \cdot 252 = 17892 \end{aligned}
python · try it yourself
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 n=50n = 50? Which growth class is it?

Answer

1275 times; O(n2)O(n^2).

Solution
The inner loop runs i times: 1+2+⋯+50=50⋅512=1275In general n(n+1)2≈n22, which is O(n2).\begin{aligned} &\text{The inner loop runs } i \text{ times: } 1 + 2 + \cdots + 50 = \frac{50 \cdot 51}{2} = 1275 \\ &\text{In general } \frac{n(n + 1)}{2} \approx \frac{n^2}{2}, \text{ which is } O(n^2). \end{aligned}

Test yourself

What is the common difference of 8, 5, 2, −1, …?

  1. 3
  2. −3-3
  3. −1-1
  4. 5

What is 1+2+3+⋯+2001 + 2 + 3 + \cdots + 200?

  1. 40000
  2. 20000
  3. 20100
  4. 20200

An inner loop runs n−1,n−2,…,1,0n - 1, n - 2, \ldots, 1, 0 times as the outer loop advances. The total is about…

  1. n2/2n^2/2
  2. nlog⁡nn \log n
  3. 2n2^n
  4. nn