What Is a Sequence?
A sequence is an ordered list of numbers, each called a term. The order matters: the sequence 1, 2, 3 is different from 3, 2, 1. We usually write the terms as a_1, a_2, a_3, ... where the small number (the subscript) is the index that tells you the position of the term.
a_1 is the first term, a_2 the second, and so on.
a_n denotes the general (nth) term at position n.
The index n is a positive integer (1, 2, 3, ...) unless stated otherwise.
Explicit Rules
An explicit (or closed-form) rule gives a_n directly as a formula in n. You can jump straight to any term without computing the ones before it. For example, a_n = 2n + 1 produces 3, 5, 7, 9, ... — just plug in n = 1, 2, 3, 4.
def term(n):
return 2 * n + 1
print([term(n) for n in range(1, 6)]) # [3, 5, 7, 9, 11]Recursive Rules
A recursive rule defines each term using one or more previous terms, plus a starting value (the base case). To find a_5 you must first know the earlier terms. The Fibonacci sequence is the classic example: a_1 = 1, a_2 = 1, and a_n = a_(n-1) + a_(n-2).
def fib(n):
a, b = 1, 1
for _ in range(n - 1):
a, b = b, a + b
return a
print([fib(n) for n in range(1, 8)]) # [1, 1, 2, 3, 5, 8, 13]Explicit vs Recursive
Explicit: fast random access to any term; needs a closed formula.
Recursive: natural for 'each term depends on the last'; must build up from the base case.
Many sequences can be written either way; the arithmetic and geometric ones have simple explicit formulas.