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.

See it run

Find the 6th term of 2, 6, 18 with three squarings, sum 1 + 2 + ... + 32 by multiplying and subtracting, shrink 64 to 1 in six halvings, watch 1 + 1/2 + 1/4 creep up to 2, and see why a doubling array appends in amortised O(1).

Operations

Find nth Term

Jump straight to any term with $a_n = a_1 r^{n-1}$ instead of walking the sequence. Here: the 6th term of $2, 6, 18, 54, \dots$

Step 1 of 7

Identify a₁, r and n

  1. 20a1
  2. 61
  3. 182
  4. 543
  5. 1624
  6. 4865
a₁ = 2r = 6 ÷ 2 = 3n = 6

The sequence is 2, 6, 18, 54, …. The first term is a_1 = 2 and every term is the previous one times 3, so the common ratio is r = 3. We want the 6th term, so n = 6. Since r > 1 the terms grow; with 0 < r < 1 they would shrink.

Step 1: Identify a₁, r and n

def nth_term(a1, r, n):    return a1 * r ** (n - 1)def fast_power(base, exp):    result = 1    while exp > 0:        if exp % 2 == 1:        # this bit of exp is set            result *= base        base *= base            # square: r, r^2, r^4, r^8 ...        exp //= 2               # move to the next bit    return resultseq = [2 * 3 ** k for k in range(6)]print(seq)                      # [2, 6, 18, 54, 162, 486]print(nth_term(2, 3, 6))        # 486print(2 * fast_power(3, 5))     # 486, in 3 rounds instead of 5 multiplications
Time
O(log n) with fast power
Space
O(1)
walk term by term
n - 1 multiplications
fast power
about log2 n squarings

The exponent is n - 1 because the first term is a1 times r to the 0. Python's built-in pow(r, e, m) already does fast power, with a modulus if you need one.

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Using r to the n for the nth term

    major

    The first term is a1 times r to the 0, so the nth term has n - 1 factors of r. Using n gives the next term along.

    Wrong
    def nth_term(a1, r, n):    return a1 * r ** n          # 6th term of 2, 6, 18 gives 1458
    Right
    def nth_term(a1, r, n):    return a1 * r ** (n - 1)    # 486
  • Forgetting the r == 1 case

    critical

    The sum formula divides by r - 1. When r is 1 every term is a1 and the sum is n times a1, but the formula raises ZeroDivisionError.

    Wrong
    def sum_gp(a1, r, n):    return a1 * (r ** n - 1) / (r - 1)   # crashes for r = 1
    Right
    def sum_gp(a1, r, n):    if r == 1:        return a1 * n    return a1 * (r ** n - 1) // (r - 1)
  • Applying the infinite sum when |r| >= 1

    critical

    a1 / (1 - r) only holds when the terms shrink. For r = 2 it returns -1 for 1 + 2 + 4 + ..., which is nonsense: the series diverges.

    Wrong
    total = 1 / (1 - 2)     # -1.0 for 1 + 2 + 4 + ...
    Right
    if abs(r) >= 1:    raise ValueError("diverges")total = a1 / (1 - r)
  • Float division for big integer sums

    major

    With / the result becomes a float, which loses exact digits past about 253. r - 1 always divides rn - 1 exactly, so integer division keeps the answer exact.

    Wrong
    sum_gp = (3 ** 60 - 1) / 2        # 2.1195...e+28, digits lost
    Right
    sum_gp = (3 ** 60 - 1) // 2       # exact integer
  • Checking the ratio with float division

    minor

    Comparing ai / a[i-1] as floats can call a true geometric sequence false (or the reverse) through rounding. Cross-multiply with integers instead.

    Wrong
    r = a[1] / a[0]ok = all(a[i] / a[i - 1] == r for i in range(2, len(a)))
    Right
    ok = all(a[i] * a[i - 2] == a[i - 1] ** 2 for i in range(2, len(a)))# valid when no term is zero
  • Growing a buffer by a fixed amount

    major

    Adding a constant number of slots on each resize makes the copy costs an arithmetic series, so n appends cost O(n²). Multiply the capacity instead.

    Wrong
    if size == capacity:    capacity += 10       # copies 10 + 20 + 30 + ...: O(n^2)
    Right
    if size == capacity:    capacity *= 2        # copies 1 + 2 + 4 + ...: under 2n

Interview tips

  • Spot it fast

    • Words like doubles, halves, triples or "times 2 each time".
    • A recursion where each call makes two or more calls: a level-by-level geometric series.
    • A loop where i *= 2 or n //= 2.
    • A resize, retry or backoff that multiplies a size or a delay.
  • Formulas to know cold

    • nth term: a1 times r to the n - 1.
    • Finite sum: a1 (r^n - 1) / (r - 1), or n a1 when r = 1.
    • Infinite sum: a1 / (1 - r), only when |r| < 1.
    • 1 + 2 + 4 + ... + 2^k = 2^(k+1) - 1.
  • Say it in the interview

    • "The work per level doubles, so the last level dominates and the total is about twice it."
    • "The resize costs are 1 + 2 + 4 + ..., which sums to under 2n, so append is amortised O(1)."
    • "The range halves each step, a geometric sequence with ratio one half, so it ends after log n steps."
  • Traps

    • The exponent is n - 1, not n.
    • r = 1 breaks the sum formula; handle it separately.
    • Do not use the infinite formula unless |r| < 1.