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 -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 and the whole sequence as . The small number is the index: it says which term you mean.
| Sequence | First terms | -th term |
|---|---|---|
| odd numbers | 1, 3, 5, 7, … | |
| squares | 1, 4, 9, 16, … | |
| powers of two | 2, 4, 8, 16, … | |
| reciprocals | ||
| alternating signs | −1, 1, −1, 1, … |
Note: Some books start counting at instead of . Python's
rangeand list indexes start at 0, so in code you will often see as the first term. Always check where a sequence starts.
Explicit and recursive rules
An explicit rule gives directly from , like . A recursive rule gives the first term and a way to get each term from the one before:
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 above, the terms are one less than powers of two, so .
# 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: and , 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 , the limit, and stay there. We write as . A sequence that does not converge diverges.
converges to 0. diverges because it grows without bound. is bounded but still diverges: it keeps jumping between and 1 and never settles.
A table of terms is the quickest way to get a feeling for a limit:
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 : , and both and 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.
# 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) / 20 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
, and .
Solution
Exercise 2: Recursive to explicit
and . Write the first five terms and an explicit rule.
Answer
2, 6, 18, 54, 162; .
Solution
Exercise 3: Limit by algebra
Find the limit of .
Answer
Solution
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 for to 15. What number do the ratios approach?
Answer
The golden ratio .
Solution
The ratios alternate above and below a value that, by solving (), is the positive root .
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?
and . What is ?
- 3
- 1
- 5
What is the limit of ?
- It diverges
- 4
- 3