A sequence formula answers "what is the 10th term?" Planning asks the other way around. In which week does the walk reach 21 km? How many steps until a token budget runs out? What decay gives a 50-step average? Each is the same formula with a different letter as the subject, and n must come out a positive whole number.
What you'll be able to do
Make n, a or d the subject of an arithmetic term formula, and l or n the subject of its sum
Solve an arithmetic sum for n as a quadratic, keeping the positive whole root
Free n with logs and r with a root in a geometric sequence, and r from an infinite sum with |r| < 1
Check that n is a positive whole number, say what it means when it isn't, and confirm with a loop
Turn around an average's window, a discounted return, a step schedule and a growing batch's token budget
Quick check
Have a guess before you start. The trail club's training plan starts with a 3 km walk and adds 2 km each week. In which week is the walk 21 km?
Week 11
Week 10
Week 9
Week 7
Show the answer
Week 10
3 + (n − 1) × 2 = 21, so (n − 1) × 2 = 18, n − 1 = 9 and n = 10. Week 1 has no extra 2 km yet, so 9 steps of 2 km lead to week 10.
The math: peel the term, solve the sum
Purpose. Make any letter of a sequence formula the subject: the position n, the first term a, the step d or the ratio r.
From lesson 26: an arithmetic sequence starts at a and adds the same step d each time, so its nth term is a + (n − 1)d. Here that term gets a name, uₙ ("u sub n"), with a subscript for its position: uₙ = a + (n − 1)d. Some books write aₙ, and call d the common difference (and a geometric sequence's r the common ratio).
Read it as a machine on n: take 1 away, multiply by d, add a. To free n, undo those in reverse: take a away, divide by d, add 1. So n = (uₙ − a)/d + 1. The other letters take fewer moves: d = (uₙ − a)/(n − 1) and a = uₙ − (n − 1)d.
Conditions. d can't be 0, or every term is the same. When you solve for d, n can't be 1. And n counts terms, so it must be a positive whole number.
Quick check
From an earlier lesson: what is the 20th term of 3, 7, 11, 15, …?
80
79
76
83
Show the answer
79
a + (n − 1)d = 3 + 19 × 4 = 79. From term 1 to term 20 there are 19 steps of 4.
Watch the club's plan built from week n = 10 and then peeled back. Each rule has an opposite, run right to left.
Try it now
Change the week and press Use these numbers: the way back always lands on the week you started from.
Symbols and formula. The sum of the first n terms is Sₙ, and l (for "last") is the last term, uₙ. Why the formula holds: write the sum forwards, then backwards under it. Each column adds to a + l, since one row goes up by d as the other goes down by d. There are n columns, so twice the sum is n(a + l). It's the pairing trick behind 1 + 2 + … + n = n(n + 1)/2, and it works for odd n too:
Sn=2n(a+l)⇒l=n2Sn−a,n=a+l2Sn
In plain words: the sum is the number of terms times the average of the first and last. To undo it, double the sum, then divide by n and take a away for l, or divide by a + l for n. Neither n nor a + l can be 0.
Named mistakes. Dividing by n instead of n − 1 when you find d: from term 1 to term n there are only n − 1 steps. Forgetting the + 1 when you find n. And dropping the 2 when you undo the halving in Sₙ.
Quick check
uₙ = a + (n − 1)d. Make d the subject.
d = (uₙ + a)/(n − 1)
d = (uₙ − a)/(n − 1)
d = (uₙ − a)/n
d = uₙ/(n − 1) − a
Show the answer
d = (uₙ − a)/(n − 1)
Take a away: uₙ − a = (n − 1)d. Divide by (n − 1): d = (uₙ − a)/(n − 1). Check with the club: (21 − 3)/9 = 2.
When n appears twice. Put l = a + (n − 1)d into Sₙ = n(a + l)/2 and you get the other sum formula, which uses d instead of the last term:
Sn=2n(2a+(n−1)d)⇒d⋅n2+(2a−d)n−2Sn=0
a and d come out with the usual moves: a = Sₙ/n − (n − 1)d/2 and d = 2(Sₙ/n − a)/(n − 1). But n sits both outside and inside the bracket, so peeling can't free it. Multiplied out, it's a quadratic in n, as in lesson 34. The club wants 120 km in total, walking 3 km and adding 2 km a week. So 2n² + 4n − 240 = 0, or n² + 2n − 120 = 0. When d and Sₙ are above 0, one root is negative and one is positive.
Geometric sequences. uₙ = a·rⁿ⁻¹ multiplies by r each step. n sits in a power, so it needs logs (lesson 35). r sits in the base, so it needs a root:
n=logrlog(un/a)+1,r=(aun)1/(n−1)
In plain words: divide by a, ask "r to what power?", then add 1. For r, take the (n − 1)th root of uₙ ÷ a.
Conditions. uₙ/a must be above 0 for the log, and r must be above 0 and not 1. For r, n can't be 1. When n − 1 is even, −r works too: r⁴ = 81 gives r = 3 or −3.
When n isn't whole. The club's newsletter has 40 readers in month 1 and 1.5 times as many each month. When does it reach 1,000? n = log 25 ÷ log 1.5 + 1 ≈ 8.94. Month 8 has 40 × 1.5⁷ ≈ 683 readers and month 9 has 40 × 1.5⁸ ≈ 1,025. So 1,000 falls between two terms, and the answer is month 9, the first at or above it.
Quick check
A geometric sequence starts at 2, and its 5th term is 162. Which values of r work?
4
3 only
20.25
3 or −3
Show the answer
3 or −3
2r⁴ = 162, so r⁴ = 81. Both 3⁴ and (−3)⁴ are 81. With r = −3 the terms are 2, −6, 18, −54, 162.
In your own words
Why does n in uₙ = a + (n − 1)d need only peeling, but n in Sₙ = n/2·(2a + (n − 1)d) needs the quadratic formula?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: solve, then build the sequence to check
Purpose. A rearranged formula can be checked by brute force: build the sequence with a loop and see where the target lands.
import math
defn_arith(u, a, d):
return (u - a) / d + 1defterm(a, d, n):
return a + (n - 1) * d
n = n_arith(21, 3, 2)
print(n, term(3, 2, n))
print(n_arith(50, 3, 2), term(3, 2, 24), term(3, 2, 25))
m = math.log(1000 / 40) / math.log(1.5) + 1print(round(m, 2), round(40 * 1.5 ** 7), round(40 * 1.5 ** 8))
It runs right here in your browser, with nothing to install.
The first line is a round trip: week 10 gives back 21. The second asks whether 50 is a term of 3, 5, 7, … It isn't: n = 24.5. 50 falls between term 24, which is 49, and term 25, which is 51. The third checks the newsletter.
Testing for whole.n == round(n) is true for 10.0 and false for 24.5. When n comes from logs, allow for tiny rounding errors: abs(n - round(n)) < 1e-9.
Worked example
Worked example: weeks to reach a sign-up total
In the club's sign-up drive, 5 people join in week 1, and each week twice as many join as the week before. A geometric sum adds them: Sₙ = a + ar + … + arⁿ⁻¹. Multiply every term by r and the list shifts one place: rSₙ = ar + ar² + … + arⁿ. Take that from Sₙ and all but two terms cancel: Sₙ − rSₙ = a − arⁿ. So Sₙ(1 − r) = a(1 − rⁿ), and Sₙ = a(1 − rⁿ)/(1 − r), for r not 1.
To free n, multiply by (1 − r) and divide by a: 1 − rⁿ = Sₙ(1 − r)/a. Then rⁿ = 1 − Sₙ(1 − r)/a, and logs free n. For a, divide instead: a = Sₙ(1 − r)/(1 − rⁿ).
import math
defn_geo_sum(s, a, r):
inside = 1 - s * (1 - r) / a
if r <= 0or r == 1or inside <= 0:
returnNonereturn math.log(inside) / math.log(r)
defa_geo_sum(s, r, n):
return s * (1 - r) / (1 - r ** n)
n = n_geo_sum(600, 5, 2)
print(round(n, 4), math.ceil(n), round(a_geo_sum(1000, 2, 6), 2))
totals = []
total = 0for week inrange(1, 9):
total = total + 5 * 2 ** (week - 1)
totals.append(total)
print(totals)
In the lesson you can change this and run it.
n ≈ 6.92, so the drive passes 600 during week 7. The loop agrees: after week 6 the total is 315, and after week 7 it's 635. The condition guards the log: the number inside must be above 0, and r must be above 0 and not 1. With r = 1 every week adds a, so Sₙ = na and n = Sₙ/a, no logs needed. For a total of 1,000 in 6 weeks, a ≈ 15.87: 16 people must join in week 1, since 15 falls short.
Quick check
What does this print?
defn_arith(u, a, d):
return (u - a) / d + 1
n1 = n_arith(40, 4, 3)
n2 = n_arith(41, 4, 3)
print(n1, n1 == round(n1), round(n2, 2), n2 == round(n2))
13 True 13.33 False
12.0 True 12.33 False
13.0 True 13.33 False
13.0 True 13.33 True
Show the answer
13.0 True 13.33 False
(40 − 4)/3 + 1 = 13.0, a whole number, so 40 is term 13. (41 − 4)/3 + 1 ≈ 13.33, not whole, so 41 isn't a term of 4, 7, 10, …
Quick check
For which of these does n = log(uₙ/a)/log r + 1 have no answer?
a = 5, r = 2, uₙ = 40
a = −5, r = 2, uₙ = −40
a = 5, r = 0.5, uₙ = 0.625
a = 5, r = 2, uₙ = −40
Show the answer
a = 5, r = 2, uₙ = −40
−40/5 = −8, and logs exist only for numbers above 0. A sequence that starts at 5 with r = 2 stays positive, so it never reaches −40.
Drill
Fix the arithmetic solvers
Each function has one slip from this lesson's named mistakes. Fix n_arith, d_arith and l_from_sum.
defn_arith(u, a, d):
return (u - a) / d
defd_arith(u, a, n):
return (u - a) / n
defl_from_sum(s, a, n):
return s / n - a
print(n_arith(21, 3, 2), d_arith(28, 4, 9), l_from_sum(150, 5, 12))
In the lesson, your code is checked as you write it.
In your own words
Why is building the sequence with a loop a good check on a rearranged formula?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: windows, horizons, schedules and budgets
Purpose. Four planning questions in training are sequence formulas turned around.
An infinite sum. In the geometric sum a(1 − rⁿ)/(1 − r), when r is between −1 and 1, rⁿ shrinks toward 0 as n grows (0.5¹⁰ is under 0.001). So adding forever gives a finite total, S∞ = a/(1 − r). Turned around, r = 1 − a/S∞. Check |r| < 1 afterward: it holds only when S∞ has the same sign as a and is more than half its size.
Moving averages. An exponential moving average (EMA) updates v = βv + (1 − β)x at every step. Optimizers like Adam use one to smooth their gradients. Next to the newest step, the steps before it count β, β², β³, … times as much, and adding them is S∞ with a = 1 and r = β: 1 + β + β² + … = 1/(1 − β). That total is the effective window, N = 1/(1 − β): roughly how many recent steps the average leans on. So β = 1 − 1/N.
A discounted return. In reinforcement learning, a steady reward r per step (here r is the reward; the ratio is γ), discounted by γ each step, is worth G = r + γr + γ²r + … = r/(1 − γ). So γ = 1 − r/G, for G not 0.
Counting decays. A step schedule, like PyTorch's StepLR, multiplies the learning rate by a factor (also called γ) every few epochs. After m decays, lr = lr₀·γᵐ, so m = log(lr/lr₀)/log γ, rounded up. Round away float dust first.
A token budget. Some large runs start with small batches and grow them. GPT-3 ramped its batch linearly from 32,000 tokens up to full size over its first 4 to 12 billion tokens. Here is a simplified model with invented, much smaller numbers: step 1 takes a tokens, and each step's batch is d tokens bigger than the last. The tokens after n steps are then an arithmetic sum, and n is its quadratic's positive root, rounded up. GPT-3's ramp wasn't set as a fixed number of tokens per step; the model keeps only the idea.
import math
defgamma_for(reward, total):
return1 - reward / total
defdecays_needed(lr, lr0, gamma):
m = math.log(lr / lr0) / math.log(gamma)
return math.ceil(round(m, 9))
defsteps_for_tokens(budget, a, d):
b = 2 * a - d
n = (-b + math.sqrt(b ** 2 + 8 * d * budget)) / (2 * d)
return math.ceil(round(n, 9))
print(gamma_for(1, 100))
print(math.log(0.0729 / 0.1) / math.log(0.9), decays_needed(0.0729, 0.1, 0.9))
print(steps_for_tokens(5000000, 32000, 1000))
total = 0for step inrange(1, 75):
total = total + 32000 + (step - 1) * 1000if step >= 73:
print(step, total)
A reward of 1 per step worth 100 in total needs γ = 0.99. Going from 0.1 to 0.0729 with γ = 0.9 gives m a speck above 3, which math.ceil alone would turn into 4; rounded first, it's 3 decays. And a 5,000,000-token budget takes 74 steps: the loop shows 73 steps fall short.
Quick check
You want an EMA that leans on about the last 20 steps. Which β?
1 − 6/10 = 0.4, 1 − 6/2 = −2.0 and 1 − 1/4 = 0.75. The −2.0 fails |r| < 1: no geometric series starting at 6 adds up to 2, so that answer must be rejected.
Drill
Plan the run
Fix beta_for_window(n), which should give β = 1 − 1/N, and steps_for_tokens(budget, a, d), which should give the first whole step at which the growing batches have used the budget.
import math
defbeta_for_window(n):
return1 / n
defsteps_for_tokens(budget, a, d):
b = 2 * a - d
n = (-b - math.sqrt(b ** 2 + 8 * d * budget)) / (2 * d)
returnround(n)
print(beta_for_window(20), steps_for_tokens(5000000, 32000, 1000))
In the lesson, your code is checked as you write it.
In your own words
Why must r = 1 − a/S∞ be checked for |r| < 1, and what does a failed check mean?
Write a sentence or two, then compare it with a good answer in the lesson.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
Learn to make any letter of any formula the subject: layers undone in reverse order, fractions, a letter that appears twice, powers, roots, quadratics, logs, inequalities, trigonometry, vectors, matrices, calculus, probability, statistics and the formulas inside AI models, each checked in Python.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write