Geometric Progressions
A geometric progression is a sequence where you multiply by the same number every step: 2, 6, 18, 54, … It models anything that grows or shrinks by a percentage: compound interest, populations, signal decay, and the clever way dynamic arrays grow. This lesson covers the -th term, the sum formula and those applications.
The common ratio
In a geometric progression (GP, geometric sequence) each term is the previous one times a fixed number , the common ratio:
| First terms | Behaviour | ||
|---|---|---|---|
| 3, 6, 12, 24, … | 3 | 2 | grows fast |
| 80, 40, 20, 10, … | 80 | shrinks toward 0 | |
| 5, −10, 20, −40, … | 5 | −2 | sign flips, size grows |
| 1, −0.3, 0.09, … | 1 | −0.3 | sign flips, shrinks |
To test a list, divide each term by the previous one: equal ratios mean a GP. If the terms blow up, if they shrink to 0, and a negative makes the signs alternate. On a graph a GP with positive terms is an exponential curve, and on a log scale it becomes a straight line (see Exponential and Logarithmic Functions).
Tip: To find how many steps a GP needs to reach a size, use logarithms: means (for ).
The sum of a GP
Let . Multiply by and subtract: almost every term cancels.
If every term equals and . A handy special case is : the sum of all the powers of two below is one less than . That is why an -bit unsigned integer holds at most .
from fractions import Fraction
def gp_sum(a1, r, n):
return a1 * (1 - r**n) / (1 - r)
print("3 + 6 + ... (10 terms):", gp_sum(3, 2, 10), sum(3 * 2**k for k in range(10)))
print("1 + 2 + ... + 2^31 :", 2**32 - 1, sum(2**k for k in range(32)))
# exact with fractions: 1 + 1/3 + 1/9 + ... (8 terms)
print("1 + 1/3 + ... (8 terms):", gp_sum(Fraction(1), Fraction(1, 3), 8))3 + 6 + ... (10 terms): 3069.0 3069
1 + 2 + ... + 2^31 : 4294967295 4294967295
1 + 1/3 + ... (8 terms): 3280/2187
Compound interest
A deposit at an annual rate , compounded once a year, is worth after years: a GP with . Compounding times per year uses rate per period and periods. Saving the same amount every year adds up a GP of balances.
P, i = 1000, 0.05
print("year simple compound")
for n in [0, 5, 10, 20, 30]:
print(f"{n:>4} {P * (1 + i * n):>6.0f} {P * (1 + i)**n:>8.2f}")
# monthly compounding for 10 years
print("monthly, 10 years:", round(P * (1 + i / 12)**(12 * 10), 2))
# save 100 at the end of every year for 10 years at 5%
total = sum(100 * (1 + i)**k for k in range(10))
print("savings plan :", round(total, 2), round(100 * ((1 + i)**10 - 1) / i, 2))year simple compound
0 1000 1000.00
5 1250 1276.28
10 1500 1628.89
20 2000 2653.30
30 2500 4321.94
monthly, 10 years: 1647.01
savings plan : 1257.79 1257.79
Simple interest is an arithmetic progression and grows along a line; compound interest is geometric and pulls away. The savings plan is the GP sum with and .
In software: dynamic array doubling
A Python list, a C++ std::vector or a Java ArrayList keeps spare room. When it is full it allocates a bigger block and copies everything over. If it doubles its capacity, the capacities are a GP (1, 2, 4, 8, …), and the total copying work to reach elements is , where is the last capacity below . By the GP sum that is . Spread over appends, that is fewer than two copies per append: amortised . Growing by a fixed amount instead (an AP of capacities) costs copies in total.
def copies(n, grow):
cap, used, work = 1, 0, 0
for _ in range(n):
if used == cap:
work += used # copy every element to the new block
cap = grow(cap)
used += 1
return work
for n in [1_000, 10_000, 100_000]:
doubling = copies(n, lambda c: 2 * c)
plus_100 = copies(n, lambda c: c + 100)
print(f"n = {n:>7}: doubling copied {doubling:>7}, +100 copied {plus_100:>10}")n = 1000: doubling copied 1023, +100 copied 4510
n = 10000: doubling copied 16383, +100 copied 495100
n = 100000: doubling copied 131071, +100 copied 49951000
Doubling's work grows like (each total is , a GP sum), while the fixed step grows like . Real implementations often use factors like 1.5 to waste less memory; any factor above 1 keeps the amortised cost constant. Other places GPs show up: exponential backoff when retrying network requests (wait 1, 2, 4, 8 s), the halving in binary search, and the shrinking learning rate in ML training schedules.
Exercises
Exercise 1: The 8th term
Find of the GP 4, 12, 36, …
Answer
8748
Solution
Exercise 2: Sum of a shrinking GP
Find the exact sum .
Answer
189
Solution
Exercise 3: Doubling time
At 7% a year compounded annually, after how many whole years does a deposit first at least double? Check with a loop.
Answer
11 years (the exact doubling time is about 10.24 years, so year 11 is the first full year past double).
Solution
import math
print(round(math.log(2) / math.log(1.07), 2))
n, balance = 0, 1.0
while balance < 2:
balance *= 1.07
n += 1
print(n, round(balance, 4))10.24
11 2.1049
Exercise 4: Backoff budget
A client retries a failed request after waiting 0.5 s, then 1 s, 2 s, 4 s, … (doubling). How long has it waited in total after 8 retries?
Answer
127.5 seconds
Solution
Test yourself
What is the common ratio of 81, −27, 9, −3, …?
- 3
Why does doubling a dynamic array's capacity give amortised appends?
- Copying is free
- The total copy work is a GP sum below , so under two copies per append on average
- The array never needs to be copied
- Because is small in practice
What is ?
- 512
- 1024
- 511
- 1023