TExcelTAD

Sequences

A sequence is an ordered list of numbers produced by a rule: a first term, a second term, a third, and so on. This lesson shows the two ways to describe a sequence (a formula for the nn-th term, or a rule that builds each term from the previous one), how to tell whether a sequence settles down, and why programmers write sequences every time they write a loop.

Terms and notation

We write the terms of a sequence as a1,a2,a3,…a_1, a_2, a_3, \ldots and the whole sequence as (an)(a_n). The small number nn is the index: it says which term you mean.

Sequence First terms nn-th term
odd numbers 1, 3, 5, 7, … an=2n−1a_n = 2n - 1
squares 1, 4, 9, 16, … an=n2a_n = n^2
powers of two 2, 4, 8, 16, … an=2na_n = 2^n
reciprocals 1,12,13,14,…1, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}, \ldots an=1/na_n = 1/n
alternating signs −1, 1, −1, 1, … an=(−1)na_n = (-1)^n

Note: Some books start counting at n=0n = 0 instead of n=1n = 1. Python's range and list indexes start at 0, so in code you will often see a0a_0 as the first term. Always check where a sequence starts.

Explicit and recursive rules

An explicit rule gives ana_n directly from nn, like an=3n+2a_n = 3n + 2. A recursive rule gives the first term and a way to get each term from the one before:

a1=5,an=an−1+3→5,8,11,14,…b1=1,bn=2⋅bn−1+1→1,3,7,15,31,…\begin{aligned} a_1 &= 5, & a_n &= a_{n-1} + 3 && \to 5, 8, 11, 14, \ldots \\ b_1 &= 1, & b_n &= 2 \cdot b_{n-1} + 1 && \to 1, 3, 7, 15, 31, \ldots \end{aligned}

Recursive rules are natural in code (each loop step updates a variable), while explicit rules let you jump straight to the 1000th term without computing the 999 before it. Finding an explicit rule for a recursive one is a common task: for bnb_n above, the terms are one less than powers of two, so bn=2n−1b_n = 2^n - 1.

python · try it yourself
# Explicit rule: jump to any term directly
def a(n):
    return 3 * n + 2

print("explicit :", [a(n) for n in range(1, 8)])

# Recursive rule: build each term from the previous one
b = 1
terms = [b]
for n in range(2, 8):
    b = 2 * b + 1
    terms.append(b)
print("recursive:", terms)
print("2^n - 1  :", [2**n - 1 for n in range(1, 8)])
explicit : [5, 8, 11, 14, 17, 20, 23]
recursive: [1, 3, 7, 15, 31, 63, 127]
2^n - 1  : [1, 3, 7, 15, 31, 63, 127]

The famous Fibonacci sequence is recursive with two previous terms: F1=F2=1F_1 = F_2 = 1 and Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2}, giving 1, 1, 2, 3, 5, 8, 13, …

Behaviour: increasing, bounded, convergent

Three questions describe how a sequence behaves:

  • Monotonic? It is increasing if every term is at least the previous one, decreasing if every term is at most the previous one.
  • Bounded? All terms stay between two fixed numbers.
  • Convergent? The terms get as close as you like to one number LL, the limit, and stay there. We write an→La_n \to L as n→∞n \to \infty. A sequence that does not converge diverges.

1/n1/n converges to 0. 2n2^n diverges because it grows without bound. (−1)n(-1)^n is bounded but still diverges: it keeps jumping between −1-1 and 1 and never settles.

A table of terms is the quickest way to get a feeling for a limit:

python · try it yourself
def a(n):
    return (2 * n + 1) / (n + 3)

for n in [1, 10, 100, 1000, 10_000, 100_000]:
    print(f"n = {n:>6}   a_n = {a(n):.6f}   distance to 2 = {abs(a(n) - 2):.6f}")
n =      1   a_n = 0.750000   distance to 2 = 1.250000
n =     10   a_n = 1.615385   distance to 2 = 0.384615
n =    100   a_n = 1.951456   distance to 2 = 0.048544
n =   1000   a_n = 1.995015   distance to 2 = 0.004985
n =  10000   a_n = 1.999500   distance to 2 = 0.000500
n = 100000   a_n = 1.999950   distance to 2 = 0.000050

The terms creep up toward 2. You can see why by dividing top and bottom by nn: an=2+1/n1+3/na_n = \frac{2 + 1/n}{1 + 3/n}, and both 1/n1/n and 3/n3/n shrink to 0. A table suggests a limit; algebra like this confirms it. The precise definition of "as close as you like" is in Sequences and, with full rigour, in Calculus in Depth.

Tip: A sequence that is increasing and bounded above always converges. That fact (the Monotone Convergence Theorem) is how many limits are shown to exist without knowing their value in advance.

In software

Every loop that updates a variable produces a sequence. A counter i += 1 is the sequence 0, 1, 2, …; a balance b *= 1.05 is a geometric sequence; Newton's method for square roots produces a sequence of guesses that converges to the answer.

python · try it yourself
# Newton's method for sqrt(10): x_{n+1} = (x_n + 10 / x_n) / 2
x = 10.0
for n in range(6):
    print(n, x)
    x = (x + 10 / x) / 2
0 10.0
1 5.5
2 3.659090909090909
3 3.196005081874647
4 3.16245562280389
5 3.162277665175675

Iterative algorithms stop when the sequence has "converged enough", typically when two successive terms differ by less than a tolerance. Python generators (yield) are a direct way to model an endless sequence and take only as many terms as you need.

Exercises

Exercise 1: Find the rule

Find an explicit rule for 4, 9, 14, 19, 24, … and compute the 50th term.

Answer

an=5n−1a_n = 5n - 1, and a50=249a_{50} = 249.

Solution
Each term is 5 more than the last, so an=5n+c.a1=4⇒5+c=4⇒c=−1a50=5⋅50−1=249\begin{aligned} &\text{Each term is 5 more than the last, so } a_n = 5n + c. \\ &a_1 = 4 \quad\Rightarrow\quad 5 + c = 4 \quad\Rightarrow\quad c = -1 \\ &a_{50} = 5 \cdot 50 - 1 = 249 \end{aligned}

Exercise 2: Recursive to explicit

c1=2c_1 = 2 and cn=3⋅cn−1c_n = 3 \cdot c_{n-1}. Write the first five terms and an explicit rule.

Answer

2, 6, 18, 54, 162; cn=2⋅3n−1c_n = 2 \cdot 3^{n-1}.

Solution
Each step multiplies by 3, starting from 2:c1=2, c2=2⋅3, c3=2⋅32, …, cn=2⋅3n−1\begin{aligned} &\text{Each step multiplies by 3, starting from 2:} \\ &c_1 = 2,\ c_2 = 2 \cdot 3,\ c_3 = 2 \cdot 3^2,\ \ldots,\ c_n = 2 \cdot 3^{n-1} \end{aligned}

Exercise 3: Limit by algebra

Find the limit of an=5n2−12n2+na_n = \frac{5n^2 - 1}{2n^2 + n}.

Answer

52=2.5\frac{5}{2} = 2.5

Solution
Divide top and bottom by n2:an=5−1/n22+1/n→5−02+0=52\begin{aligned} &\text{Divide top and bottom by } n^2{:} \\ &a_n = \frac{5 - 1/n^2}{2 + 1/n} \quad\to\quad \frac{5 - 0}{2 + 0} = \frac{5}{2} \end{aligned}
python · try it yourself
for n in [10, 1000, 100_000]:
    print(n, (5 * n**2 - 1) / (2 * n**2 + n))
10 2.376190476190476
1000 2.4987501249375312
100000 2.4999875000125

Exercise 4: Fibonacci ratios

Write a loop that prints Fn+1/FnF_{n+1} / F_n for n=1n = 1 to 15. What number do the ratios approach?

Answer

The golden ratio φ=1+52≈1.618034\varphi = \frac{1 + \sqrt{5}}{2} \approx 1.618034.

Solution

The ratios alternate above and below a value that, by solving r=1+1/rr = 1 + 1/r (r2−r−1=0r^2 - r - 1 = 0), is the positive root 1+52\frac{1 + \sqrt{5}}{2}.

python · try it yourself
import math

a, b = 1, 1
for n in range(1, 16):
    a, b = b, a + b
print(b / a)
print((1 + math.sqrt(5)) / 2)
1.618034447821682
1.618033988749895

Test yourself

Which sequence is bounded but does not converge?

  1. an=1/na_n = 1/n
  2. an=(−1)na_n = (-1)^n
  3. an=n2a_n = n^2
  4. an=3a_n = 3

a1=7a_1 = 7 and an=an−1−2a_n = a_{n-1} - 2. What is a4a_4?

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

What is the limit of an=3n+4n−1a_n = \frac{3n + 4}{n - 1}?

  1. It diverges
  2. −4-4
  3. 4
  4. 3