AlgoViz

Geometric sequences

easy 15 min

A geometric sequence multiplies by the same ratio every step: 1, 2, 4, 8 or 64, 32, 16, 8.

Video lesson · Chapter 1 of 5

The recorder fills unevenly

0:00 / 4:18

Geometric sequences

1:22Reel

Read caption

Otto doubles a photo array, then accounts for fifteen moves across its first nine appends. Double the space. One copy buys a run of cheap writes.

How it works

Step through the idea, or press Play.

Step 1 of 3

Multiply by r. Each term is the previous one times a fixed ratio r: 2, 6, 18, 54 has r = 3

Understand it

Start here

What a geometric sequence is

A geometric sequence (also called a geometric progression, GP) is a list of numbers where each term is the previous term multiplied by the same constant, the common ratio rr. Start at 2 and keep multiplying by 3: 2,6,18,54,162,…2, 6, 18, 54, 162, \dots. Divide any term by the one before it and you always get 3.

The pattern

a1,  a1r,  a1r2,  a1r3,  …r=ak+1aka_1,\; a_1 r,\; a_1 r^2,\; a_1 r^3,\; \dots \qquad r = \frac{a_{k+1}}{a_k}
a1a_1 is the first term and rr the ratio between any two neighbours. The power of rr on a term is one less than its position.
  • Growth: r>1r > 1

    Each term is bigger: 2,4,8,16,322, 4, 8, 16, 32 with r=2r = 2. Recursion trees and doubling arrays.

  • Decay: 0<r<10 < r < 1

    Each term is smaller: 32,16,8,4,232, 16, 8, 4, 2 with r=12r = \frac12. Binary search and halving loops.

  • Flat: r=1r = 1

    Every term is the same: 5,5,55, 5, 5. The sum is just n⋅a1n \cdot a_1.

  • Alternating: r<0r < 0

    Signs flip each step: 1,−2,4,−81, -2, 4, -8 with r=−2r = -2.

Add or multiply

Arithmetic against geometric

An arithmetic sequence adds the same amount each step, so it grows in a straight line. A geometric sequence multiplies by the same amount, so it grows (or shrinks) exponentially. The difference decides whether a total is O(n2)O(n^2) or O(n)O(n), and whether a loop runs nn times or log⁡n\log n times.

ArithmeticGeometric
Ruleadd dd: ak+1=ak+da_{k+1} = a_k + dmultiply by rr: ak+1=ak⋅ra_{k+1} = a_k \cdot r
Example3,5,7,93, 5, 7, 9 (d=2d = 2)3,6,12,243, 6, 12, 24 (r=2r = 2)
nth terma1+(n−1)da_1 + (n-1)da1rn−1a_1 r^{n-1}
Sum of nn termsn(a1+an)2\frac{n(a_1 + a_n)}{2}, quadratic in nna1rn−1r−1a_1 \frac{r^n - 1}{r - 1}, dominated by the last term
Shapestraight lineexponential curve
In codei += 2: O(n)O(n) iterationsi *= 2: O(log⁡n)O(\log n) iterations

Formula 1

Jump to any term

The nth term

an=a1 r n−1a_n = a_1 \, r^{\,n-1}
To reach position nn you multiply a1a_1 by rr exactly n−1n - 1 times, once per gap between terms.

Worked example. Find the 6th term of 2,6,18,54,…2, 6, 18, 54, \dots. Here a1=2a_1 = 2, r=6/2=3r = 6 / 2 = 3 and n=6n = 6, so a6=2⋅35=2⋅243=486a_6 = 2 \cdot 3^5 = 2 \cdot 243 = 486. Check by walking: 2, 6, 18, 54, 162, 486. Another: with a1=3a_1 = 3 and r=2r = 2, a6=3⋅25=96a_6 = 3 \cdot 2^5 = 96.

Python
def nth_term(a1, r, n):    return a1 * r ** (n - 1)terms = [2]for _ in range(5):    terms.append(terms[-1] * 3)    # multiply by r each stepprint(terms)                 # [2, 6, 18, 54, 162, 486]print(nth_term(2, 3, 6))     # 486print(nth_term(3, 2, 6))     # 96print(nth_term(1, 2, 11))    # 1024 = 2**10
The formula, the walk, and a check that they agree.
Edit and run it in your browser

Fast power

Computing r to the n in O(log n)

The formula needs rn−1r^{n-1}. Multiplying rr by itself n−1n - 1 times is O(n)O(n). Binary exponentiation (fast power) reads the exponent in binary instead: square the base each round, and multiply it into the result whenever the current bit is 1. The exponent halves every round, so there are only about log⁡2n\log_2 n rounds.

Squaring instead of repeating

35=34+1=(32)2⋅31=81⋅3=2433^5 = 3^{4+1} = \left(3^2\right)^2 \cdot 3^1 = 81 \cdot 3 = 243
5=10125 = 101_2: bits 0 and 2 are set, so the answer is 31⋅343^1 \cdot 3^4. Two squarings reach 343^4; for an exponent near a billion it takes about 30 rounds.

The fast power loop

Each pass halves exp, so the loop runs about log⁡2n\log_2 n times. Python's pow(r, n, m) does this for you, with an optional modulus.

Formula 2

Adding the first n terms

Adding terms one by one is O(n)O(n). The multiply-and-subtract trick gives a closed form. Write the sum SS, write rSrS underneath, and notice that rSrS is the same list shifted one place. Subtract, and everything in the middle cancels.

The multiply-and-subtract trick

S=a1+a1r+a1r2+⋯+a1rn−1rS=a1+a1r+a1r2+⋯+a1rn−1+a1rnrS−S=a1rn−a1\begin{aligned} S &= a_1 + a_1 r + a_1 r^2 + \dots + a_1 r^{n-1} \\ rS &= \phantom{a_1 + {}} a_1 r + a_1 r^2 + \dots + a_1 r^{n-1} + a_1 r^{n} \\ rS - S &= a_1 r^n - a_1 \end{aligned}
Only the first term of SS and the last term of rSrS survive the subtraction.

The finite sum

Sn=a1 rn−1r−1(r≠1),Sn=n a1(r=1)S_n = a_1 \, \frac{r^n - 1}{r - 1} \quad (r \ne 1), \qquad S_n = n \, a_1 \quad (r = 1)
For 2+6+18+54+162+4862 + 6 + 18 + 54 + 162 + 486: S6=2⋅36−12=728S_6 = 2 \cdot \frac{3^6 - 1}{2} = 728. Equivalent form for ∣r∣<1|r| < 1: a11−rn1−ra_1 \frac{1 - r^n}{1 - r}.
The trick in four moves
  1. 1Write S1 + 2 + 4 + 8 + 16 + 32
  2. 2Multiply by r2S = 2 + 4 + 8 + 16 + 32 + 64
  3. 3Subtract2S - S = 64 - 1, the middle cancels
  4. 4SolveS = 63 = 2^6 - 1

The identity to memorise

1 + 2 + 4 + ... + 2^k = 2^(k+1) - 1

Powers of two

1+2+4+⋯+2k=2k+1−1<2⋅2k1 + 2 + 4 + \dots + 2^k = 2^{k+1} - 1 < 2 \cdot 2^k
The sum of all the powers of two up to 2k2^k is one less than the next power, so it is always less than twice the last term.

You can see it in binary too: 1+2+4+8+16+321 + 2 + 4 + 8 + 16 + 32 is 111111, and adding 1 gives 1000000 =26=64= 2^6 = 64, so the sum is 6363. Whenever work doubles from one level to the next, the last level is bigger than all the earlier levels combined.

Where the identity shows up

  • Binary tree nodes

    A full tree of height hh has 1+2+⋯+2h=2h+1−11 + 2 + \dots + 2^h = 2^{h+1} - 1 nodes, and about half are leaves.

  • Recursion calls

    A function that calls itself twice for nn levels makes 2n−12^n - 1 calls in total.

  • Array resizes

    Doubling from 1 to 2k2^k copies 1+2+⋯+2k−1=2k−11 + 2 + \dots + 2^{k-1} = 2^k - 1 elements.

  • Bit masks

    (1 << (k + 1)) - 1 is k+1k + 1 ones in binary, the same number.

Formula 3

Infinite series that add up

When ∣r∣<1|r| < 1 the terms shrink towards 0, and rn→0r^n \to 0 in the sum formula. So infinitely many terms add up to a finite number: 1+12+14+18+⋯=21 + \frac12 + \frac14 + \frac18 + \dots = 2. Each new term covers exactly half of the gap that is left, so the total creeps towards 2 and never passes it.

The infinite sum

S∞=a11−ronly when ∣r∣<1S_\infty = \frac{a_1}{1 - r} \quad \text{only when } |r| < 1
1+0.5+0.25+⋯=11−0.5=21 + 0.5 + 0.25 + \dots = \frac{1}{1 - 0.5} = 2. With a1=2a_1 = 2 it is 4.

Does the infinite sum exist?

With r=2r = 2 the partial sums 1, 3, 7, 15 grow forever; with r=−1r = -1 they bounce 1, 0, 1, 0.

Halving

Binary search is a shrinking sequence

Binary search keeps half of the range after every comparison: 64,32,16,8,4,2,164, 32, 16, 8, 4, 2, 1. That is a geometric sequence with a1=na_1 = n and r=12r = \frac12. The number of steps is the number of terms before it reaches 1.

Counting the halvings

n2k=1  ⟹  2k=n  ⟹  k=log⁡2n\frac{n}{2^k} = 1 \;\Longrightarrow\; 2^k = n \;\Longrightarrow\; k = \log_2 n
64 items take 6 halvings to reach one candidate, and one more comparison checks it: at most ⌊log⁡2n⌋+1=7\lfloor \log_2 n \rfloor + 1 = 7 comparisons.
Items nnHalvings to reach 1Worst-case comparisonsLinear search
646764
1,02410111,024
1,000,00019201,000,000
1,000,000,00029301,000,000,000
Doubling nn adds one halving. The same halving drives fast power, merge sort's depth and balanced tree height.

Doubling

Why dynamic array append is amortised O(1)

A Python list or Java ArrayList keeps spare capacity. When it is full, it allocates a bigger block and copies every element across, which costs O(n)O(n) for that one append. If the capacity doubles, the copy costs over the array's life are 1,2,4,8,…1, 2, 4, 8, \dots: a geometric sequence.

Total copies for n appends

1+2+4+⋯+2k=2k+1−1<2n(2k<n)1 + 2 + 4 + \dots + 2^{k} = 2^{k+1} - 1 < 2n \qquad (2^k < n)
The biggest copy is smaller than nn, and the whole sum is under twice the biggest copy. So nn appends do fewer than n+2n=3nn + 2n = 3n units of work: O(1)O(1) each on average.

One append

Resizes are rare and get rarer: after copying kk elements, the next kk appends are free.

Grow by a factor (x2, x1.5)

  • Copy costs 1+2+4+…1 + 2 + 4 + \dots: geometric, under 2n2n.
  • Append is amortised O(1)O(1).
  • Used by Python lists, Java ArrayList, C++ vector.

Grow by a constant (+1, +10)

  • Copy costs 10+20+30+…10 + 20 + 30 + \dots: arithmetic, about n220\frac{n^2}{20}.
  • Append becomes O(n)O(n) on average, O(n2)O(n^2) in total.
  • A classic cause of slow string building.

Exponential time

Doubling recursion is O(2^n)

Naive recursive Fibonacci calls itself twice per call. Level 0 has 1 call, level 1 has 2, level 2 has 4, and level kk up to 2k2^k: a geometric sequence with r=2r = 2. Summing the levels gives the total number of calls.

Calls double at every level

fib(2) is computed twice already. With nn levels the tree has up to 2n−12^n - 1 calls, which memoisation cuts to O(n)O(n).

Total calls for n levels

1+2+4+⋯+2n−1=2n−1=O(2n)1 + 2 + 4 + \dots + 2^{n-1} = 2^n - 1 = O(2^n)
The same sum counts subsets of nn items and the moves in Tower of Hanoi (2n−12^n - 1).

Pattern spotting

Reading complexity from the ratio

Which geometric pattern is this?

A loop with i *= 2 reaches nn in log⁡2n\log_2 n steps; a recursion that doubles its calls per level does O(2n)O(2^n) work.

The complexity connection

  • O(log n): halving, r = 1/2

    Binary search, fast exponentiation, merge sort depth, balanced tree height.

  • O(2ⁿ): doubling, r = 2

    Naive recursion, subset generation, Tower of Hanoi.

  • Amortised O(1)

    Doubling arrays and hash table resizes: a geometric sum under 2n2n.

  • O(n) total from shrinking work

    Work n+n2+n4+⋯<2nn + \frac{n}{2} + \frac{n}{4} + \dots < 2n, as in building a heap or quickselect on average.

Recap

Key takeaways

Key points

  • A geometric sequence multiplies by a fixed ratio rr: an=a1rn−1a_n = a_1 r^{n-1}.
  • The finite sum is Sn=a1rn−1r−1S_n = a_1 \frac{r^n - 1}{r - 1}, from multiplying by rr and subtracting; when r=1r = 1 it is na1n a_1.
  • 1+2+4+⋯+2k=2k+1−11 + 2 + 4 + \dots + 2^k = 2^{k+1} - 1: a doubling series is less than twice its last term.
  • When ∣r∣<1|r| < 1 the infinite sum is a11−r\frac{a_1}{1 - r}; otherwise it diverges.
  • Halving (r = 1/2) gives O(log⁡n)O(\log n) steps; doubling work per level (r = 2) gives O(2n)O(2^n).
  • Doubling a dynamic array copies under 2n2n elements in total, so append is amortised O(1)O(1).
  • Compute large powers with fast exponentiation in O(log⁡n)O(\log n), and keep integer sums exact with //.

Sources