Some formulas hold the letter you want both squared and plain, like a ball's height h = ut − ½gt², where t appears as t² and as t. Gathering can't make it appear once, and a single square root can't undo it. Transposing it takes three steps: rearrange the formula into the standard shape at² + bt + c = 0, use the quadratic formula, and choose the answer that makes sense. A second route, peeling a completed square, shows where that formula comes from.
What you'll be able to do
Recognize a formula where the letter appears both squared and plain, so peeling and gathering can't free it
Rearrange such a formula into the shape ax² + bx + c = 0 and read off a, b and c, even when they are letters
Make the letter the subject with the quadratic formula, and use the discriminant to say how many answers exist
Undo a completed square, y = a(x − p)² + q, by peeling it, keeping the ±
Choose the root that makes sense, and size a sequence length from a budget that grows with its square
Quick check
Have a guess before you start. A ball thrown upward has height h = 20t − 5t² meters after t seconds. When is it 15 m up?
Never
At t = 1 and at t = 3
At t = 1 only
At t = 0.75
Show the answer
At t = 1 and at t = 3
20t − 5t² = 15 gives 5t² − 20t + 15 = 0, so t² − 4t + 3 = 0 and t = 1 or 3. The ball passes 15 m on the way up and again on the way down.
The math: rearrange into a quadratic, then solve
Purpose. Free a letter that appears both squared and plain.
Move every term to one side, so the other side is 0.
Arrange it as a·x² + b·x + c = 0 and read off a, b and c. They can be numbers or other letters.
Use the quadratic formula: x = (−b ± √(b² − 4ac))/(2a).
Look at the discriminant, b² − 4ac. Above 0 there are two answers, exactly 0 gives one, and below 0 gives none.
Keep the answers that make sense for the question.
For the ball, h = ut − ½gt² becomes ½g·t² − u·t + h = 0. So a = g/2, b = −u and c = h, and the formula gives t = (u ± √(u² − 2gh))/g. One formula now answers "when is the ball at height h?" for any throw.
Quick check
From an earlier lesson: what are the roots of x² − 5x + 6 = 0?
2 and 3
5 and 6
1 and 6
−2 and −3
Show the answer
2 and 3
It factors as (x − 2)(x − 3) = 0, so x = 2 or x = 3. Check: 4 − 10 + 6 = 0 and 9 − 15 + 6 = 0.
Watch the formula answer "when is the ball 15 m up?" Rearranged, 20t − 5t² = 15 is −5t² + 20t − 15 = 0, so a = −5, b = 20 and c = −15.
Try it now
Set c to −25 (the question "when is it 25 m up?") and press Use these numbers: the discriminant goes below 0.
Symbols and formula. A second route: if the formula is a completed square, the letter appears once, inside a bracket, and you can peel it like any other formula.
y=a(x−p)2+q⇒ay−q=(x−p)2⇒x=p±ay−q
In plain words: take q away, divide by a, take the square root with its ±, then add p.
Writing ax² + bx + c in this shape is called completing the square. Doing it to the general quadratic and then peeling gives exactly the quadratic formula. So the formula is just a peeling, done once for every quadratic.
Quick check
Make x the subject of y = 2(x − 3)² + 1.
x = 3 ± √((y − 1)/2)
x = (y − 1)/2 + 3
x = √((y − 1)/2) − 3
x = 3 + (y − 1)/4
Show the answer
x = 3 ± √((y − 1)/2)
Take 1 away, divide by 2, take the root with ±, then add 3. With y = 19: (19 − 1)/2 = 9, √9 = 3, so x = 0 or x = 6.
When a, b and c are letters. A can's surface is S = 2πr² + 2πrh, and r appears squared and plain. Move S across: 2π·r² + 2πh·r − S = 0, so a = 2π, b = 2πh and c = −S. The formula gives r = (−2πh + √(4π²h² + 8πS))/(4π), and dividing top and bottom by 2π tidies it to r = (−h + √(h² + 2S/π))/2. Only the + root is kept, since the − root is always negative. The method doesn't care whether a, b and c are numbers or letters: read them off, then put them into the formula.
Choosing the root. The formula gives every number that fits. The question decides which ones you keep. A time after the throw, a length and a count of anything can't be negative. Sometimes both roots are real answers, like the ball's two times. And if a = 0, there's no square at all: the formula would divide by 0, and you're back to the reverse-order method of the first lesson in this subcourse.
In your own words
Why can't the reverse-order method free t from h = ut − ½gt² on its own?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: a function that returns every root
Purpose. Code that solves a quadratic must handle all three cases: two roots, one, or none.
import math
defroots(a, b, c):
d = b * b - 4 * a * c
if d < 0:
return []
if d == 0:
return [-b / (2 * a)]
r = math.sqrt(d)
return [(-b - r) / (2 * a), (-b + r) / (2 * a)]
print(roots(1, -4, 3), roots(1, -4, 4), roots(1, -4, 5))
print(roots(5, -20, 15))
It runs right here in your browser, with nothing to install.
A list fits the job: it can hold two answers, one, or none. The empty list for x² − 4x + 5 is the honest answer: no real number makes it 0. Checking d < 0 before taking the root also avoids math.sqrt's error.
Worked example
Worked example: when is the ball at a given height?
t from h = ut − ½gt², with g rounded to 10, and a round trip through the height formula.
import math
deftimes_at_height(u, g, h):
d = u * u - 2 * g * h
if d < 0:
return []
r = math.sqrt(d)
return [(u - r) / g, (u + r) / g]
defheight(u, g, t):
return u * t - g * t * t / 2print(times_at_height(20, 10, 15), times_at_height(20, 10, 25))
print(height(20, 10, 1), height(20, 10, 3))
In the lesson you can change this and run it.
The rearranged formula t = (u ± √(u² − 2gh))/g gives 1.0 and 3.0 seconds for 15 m, and an empty list for 25 m, which the ball never reaches. Putting both times back into the height formula gives 15.0 each time: the round trip. Here d = u² − 2gh is the discriminant of ½g·t² − u·t + h = 0, simplified.
Quick check
What does this print?
import math
defroots(a, b, c):
r = math.sqrt(b * b - 4 * a * c)
return [(-b - r) / (2 * a), (-b + r) / (2 * a)]
print(roots(2, -2, -12))
The round trip still works. A quadratic rearrangement has more places to slip than any before: a sign in b, a bracket around 2a, the wrong root. The check is the one you know. Put every root back into the original formula and confirm it gives the value you asked for, as the example did with height(20, 10, 1) and height(20, 10, 3). Then use the question, not the formula, to decide which roots to report.
Quick check
roots(1, 3, -40) gives [−8.0, 5.0] for a rectangle's width. What do you report?
5 m
Neither: the equation is wrong
Both
−8 m
Show the answer
5 m
Both numbers solve w² + 3w − 40 = 0, but a width can't be negative, so only 5 m answers the question.
Drill
Fix the quadratic formula
roots(a, b, c) has a bracket missing, and it crashes when there are no real roots. Fix both, so it returns [] when the discriminant is below 0.
import math
defroots(a, b, c):
d = b * b - 4 * a * c
r = math.sqrt(d)
return [(-b - r) / 2 * a, (-b + r) / 2 * a]
print(roots(2, -2, -12))
In the lesson, your code is checked as you write it.
In your own words
Why does roots return a list rather than a single number?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: costs that grow with the square
Purpose. In machine learning, quadratics appear wherever a cost grows with the square of a size.
How long can a sequence be? Attention compares every token (a word or piece of a word) with every other, so its memory grows like n² for n tokens. Add a part that grows like n·d, for vectors of length d, and the cost is n² + d·n. To find the longest sequence a budget B allows, solve n² + d·n − B = 0 and keep the positive root, rounded down: n = (−d + √(d² + 4B))/2.
Two weights with the same loss. A one-weight loss shaped like a bowl, L(w) = (w − 3)² + 1, is lowest at w = 3. Make w the subject and you get w = 3 ± √(L − 1). A loss of 5 comes from w = 1 and from w = 5: the loss tells you how far you are from the best weight, but not which side you're on.
import math
defmax_tokens(d, budget):
return math.floor((-d + math.sqrt(d * d + 4 * budget)) / 2)
defweights_for_loss(loss, best, floor):
if loss < floor:
return []
r = math.sqrt(loss - floor)
return [best - r, best + r]
n = max_tokens(64, 1000000)
print(n, n * n + 64 * n, weights_for_loss(5, 3, 1), weights_for_loss(0.5, 3, 1))
Quick check
L(w) = (w − 3)² + 1. Which weights give a loss of 10?
6 only
0 and 6
−6 and 6
3 and 10
Show the answer
0 and 6
w = 3 ± √(10 − 1) = 3 ± 3, so w = 0 or w = 6. Check: (0 − 3)² + 1 = 10.
Quick check
What does this print?
import math
defmax_tokens(d, budget):
return math.floor((-d + math.sqrt(d * d + 4 * budget)) / 2)
print(max_tokens(64, 1000000), max_tokens(128, 1000000))
968.0 938.0
968 938
1000 1000
938 968
Show the answer
968 938
With d = 64 the budget allows 968 tokens; doubling d to 128 leaves room for only 938. math.floor returns a whole number, so there's no .0.
Drill
A budget and a loss, turned around
Write max_tokens(d, budget) (the largest whole n with n² + d·n ≤ budget) and weights_for_loss(loss, best, floor) for L(w) = (w − best)² + floor, returning [] when the loss is below the floor.
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