Please enable JavaScript to use CodeHS

Coding for High School Math

Coding for Sequences and Series

Patterns of numbers show up everywhere, from loan payments to population growth. Code lets you generate terms, add them up, and watch how a sequence behaves as it grows.

High School Math ~12 min Algebra 2 / Precalculus
Arithmetic

Arithmetic sequences

An arithmetic sequence adds the same amount each time. If the first term is a₁ and the common difference is d, then the nth term is:

aₙ = a₁ + (n - 1)d

For example, 3, 7, 11, 15... has a₁ = 3 and d = 4. The 10th term is 3 + 9·4 = 39.

Geometric

Geometric sequences

A geometric sequence multiplies by the same factor each time. If the common ratio is r, the nth term is:

aₙ = a₁ · r^(n-1)

For example, 2, 6, 18, 54... has a₁ = 2 and r = 3. The 6th term is 2 · 3⁵ = 486.

Key idea: Arithmetic sequences add, geometric sequences multiply. The code is almost identical; only the operation changes.

Sums

Partial sums and infinite series

A series is the sum of sequence terms. For an arithmetic sequence, the sum of the first n terms is:

Sₙ = n/2 · (a₁ + aₙ)

For a geometric sequence with r ≠ 1, the finite sum is:

Sₙ = a₁(1 - rⁿ) / (1 - r)

When |r| < 1, an infinite geometric series converges to:

S = a₁ / (1 - r)

Recursion

Recursion and Fibonacci

Some sequences are defined by recursion: each term depends on previous terms. The Fibonacci sequence is:

F(1) = 1, F(2) = 1, F(n) = F(n-1) + F(n-2)

Recursion is natural in code because a function can call itself. For long sequences, an iterative loop is usually faster, but the recursive definition matches the math directly.

Interactive

Interactive sequence builder

Pick a sequence type, set the first term and common difference or ratio, and choose how many terms to generate. The tool also computes the partial sum.


    
Code It

Sequences and series in Python

These functions mirror the formulas exactly. Arithmetic uses addition, geometric uses multiplication, and the sum helper works for any list of numbers.

sequences.py
def arithmetic_term(a1, d, n):
    return a1 + (n - 1) * d

def geometric_term(a1, r, n):
    return a1 * r**(n - 1)

def geometric_sum(a1, r, n):
    return a1 * (1 - r**n) / (1 - r)

def fibonacci(n):
    if n <= 2:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)

print(arithmetic_term(3, 4, 10))  # 39
print(geometric_sum(2, 3, 5))     # 242
print(fibonacci(8))           # 21
Output
39
242.0
21

Each function is small and predictable. When you understand the formula, writing the code is mostly a matter of turning math notation into Python operators.

Check Yourself

Quick quiz

1. In an arithmetic sequence, what stays constant?

2. What formula gives the nth term of a geometric sequence?

3. Which value of r makes an infinite geometric series converge?

Keep learning

Sequences show how quantities grow and shrink. Next, use exponents and logarithms to model growth rates and solve for time.

Part of the “Coding for High School Math” series.