Skip to Content
Chapter 1

Computation and cost: What counts as fast

Tomas Reimers 13 minute read

In March 1956, Kurt Gödel wrote a letter to John von Neumann, who was dying of cancer in a Washington hospital. Buried in the condolences is a question. Gödel imagines a machine that, given a mathematical statement and a number nn, decides whether the statement has a proof of length nn — and asks how the number of steps the machine needs, call it φ(n)\varphi(n), has to grow. Brute force gives something like cnc^n: a proof of length nn is a string of nn symbols from some fixed alphabet of cc characters, and a machine can try every string.

But Gödel saw no reason that had to be optimal, and he saw exactly what was at stake if it wasn’t. If φ(n)\varphi(n) could be brought down to nn or n2n^2, he wrote, “the mental work of a mathematician concerning Yes-or-No questions could be completely replaced by a machine” — despite the famous undecidability results, his own included. (Those results forbid a machine that answers every mathematical question; they say nothing against finding proofs of a bounded length quickly, which is a finite (merely enormous) search.) Then he asked von Neumann what he thought, and the question sat unread in the literature for thirty years. It is, almost exactly, the question this book is about: the letter  is P vs NP, posed fifteen years before either class had a name.

To even state Gödel’s question precisely, you need three things, and they’re this chapter’s arc:

  1. A definition of a step — some machine so simple that counting its operations is unambiguous, and an argument that the choice of machine doesn’t matter.
  2. A definition of cost — how running time is measured as a function of input size, and why the measurement deliberately ignores constants and hardware.
  3. A definition of fast — the class P, the field’s formalization of “feasible,” along with the caveats about where that formalization creaks.

By the end we’ll also meet the first genuine theorem of the subject (a proof that more time really does buy more power) and see precisely why that theorem, strong as it is, says nothing about the question in Gödel’s letter.

The machine: what a step is

Every programming language you’ve used compiles down to something, and if you keep compiling (through bytecode and below) you eventually want a bottom: a machine so primitive that nobody can argue about what one operation costs. The field’s chosen bottom is the Turing machine : an infinite tape of cells, a read/write head sitting on one cell, and a fixed finite table of rules of the form “in this state, reading this symbol: write that symbol, move one cell left or right, switch to that state.” That’s the entire machine. Here is a complete, running example — a Turing machine that adds 1 to a binary number, using one state and three rules:

read 0 → write 1, haltread 1 → write 0, move leftread ␣ → write 1, halt
… ␣1011␣ …
state: carry · head at cell 4
the setup: tape holds 1011 (eleven in binary), head on the rightmost bit, state "carry"
A Turing machine incrementing a binary number — the trace is the real execution of the three-rule table shown. Any computation, on any computer, decomposes into steps this small; counting them is how cost is defined.

Watch what a “step” is: read one cell, consult one rule, write one cell, move one position. A step is local, touching a bounded amount of information — and that locality is the point of the definition. In Chapter 2, the proof that crowns Boolean satisfiability (SAT, the puzzle-problem this book orbits) as its master problem works precisely because each step of any computation is this small.

The immediate objection: your laptop is not a Turing machine. It has random-access memory, registers, a cache hierarchy — surely “number of steps” differs between the models? It does, and the answer is the foundational result that makes complexity theory possible: every reasonable model of computation simulates every other with at most polynomial overhead (call the set of such models the club of reasonable machines). A RAM machine step becomes polynomially many Turing machine steps (the head must walk the tape to reach an address, so the cost grows with the memory in use — but it never explodes). A Python statement becomes a bounded pile of RAM operations. And so on around the club: an algorithm taking time TT on your laptop takes time at most, say, T3T^3 on a Turing machine — never 2T2^T.

One piece of fine print, which will matter in Part III: the equivalences are free only at this coarse resolution. Price them finely and the models genuinely disagree; a one-tape Turing machine provably needs n2n^2 steps to recognize palindromes that a two-tape variant (one extra tape, one extra head) handles in linear time. The Church–Turing thesis says all the machines agree about what can be computed. Complexity theory is the discovery that they never stop arguing about how much, and the arguments are where the field’s few unconditional theorems come from.

SIMULATE EACH OTHER WITH POLYNOMIAL OVERHEADTuring machine↔RAM model↔your laptop↔Python interpreterquantum computer?
The club of reasonable machines: each simulates any other with at most polynomial overhead, so 'polynomial time' means the same thing in all of them. The one serious applicant waiting outside is the quantum computer (Chapter 3 returns to it).

This is why the definitions coming next are phrased to be blind to polynomial differences: any finer distinction than “polynomial vs not” would depend on which machine you bought. The claim that this club contains every physically buildable computer — that nothing in nature computes with super-polynomial advantage over a Turing machine — is called the extended Church–Turing thesis , and one caveat right away: quantum computers seriously challenge it. A quantum computer appears to factor integers exponentially faster than any known classical method. What that does and doesn’t mean for this book’s question gets treated properly in Chapter 3; until then, “computer” means anything in the club.

The cost: counting steps

Fix a problem — say, multiplying two numbers. An algorithm’s running time is a function: for each input size nn (measured in bits, or digits, or any polynomially-equivalent unit), report the number of steps on the worst input of that size:

T(n)  is the step count of the  worst case\tip{running time at input size n}{T(n)} \;\text{is the step count of the}\; \tip{the slowest input among all inputs of that size}{\text{worst case}}

Why worst case, rather than average? Partly because it’s the only definition that doesn’t force you to choose a probability distribution over inputs (average over what? — the inputs your users send are not uniform random bits). Partly because a worst-case guarantee is what an adversary can’t break, which is what cryptography (Chapter 3) and verification (Chapter 4) need. It’s a genuinely consequential choice, and it has a sting in its tail: a problem can be worst-case hard while almost every instance you’d ever meet is easy. That gap (worst case versus typical case) becomes the central plot of Part II, and it was baked in on page one.

With worst case fixed, we blur the measurement on purpose — twice. The first blur: report T(n)T(n) up to constant factors and lower-order terms — O(n2)O(n^2) rather than 3n2+17n3n^2 + 17n steps — because constants are what chip speed and implementation details scramble (a 2× faster core halves every count), so keeping them would mean measuring your hardware rather than your algorithm. The second blur, coarser, comes with the next section’s definition: caring only about polynomial versus not, which is what absorbs the machine club’s polynomial simulation overheads. What survives both blurs is the growth rate, and the growth rate is what decides everything:

input size n:
1 secondage of universe10⁵² years10¹³² years0.66 μsn log n10 μsn²1.0 msn³10^14 years2ⁿ10^141 yearsn!
Runtime at one operation per nanosecond, on a log scale — each step up the axis is many orders of magnitude slower (the chart re-scales as n changes; watch the axis, not just the bars). At n = 100, an n² algorithm finishes in 10 μs, while 2ⁿ takes 10^14 years. The universe is 1.38 × 10¹⁰ years old.

The chart’s y-axis is logarithmic (each gridline is a leap by orders of magnitude — the only way to fit these numbers in one picture). At n=100n = 100, one operation per nanosecond throughout: an n2n^2 algorithm finishes in 10 microseconds, n3n^3 in a millisecond, and 2n2^n needs 1.3×10301.3 \times 10^{30} operations, which is 4×10134 \times 10^{13} years — about 2,900 times the age of the universe. Exponentials compound mercilessly: moving from n=100n = 100 to n=300n = 300 leaves n2n^2 at 90 microseconds while 2n2^n goes from “thousands of universe-lifetimes” to 107310^{73} years. No conceivable hardware rescues this. Throw a billion-core datacenter at 2n2^n and you’ve bought yourself 30 more points of nn, because 230≈1092^{30} \approx 10^9: the boundary between polynomial and exponential moves by addition when your budget moves by multiplication.

Make it concrete with the two problems that will haunt this entire book. Multiplying two 300-digit numbers by the grade-school method costs about 3002=90,000300^2 = 90{,}000 digit-operations — 90 microseconds. Now reverse it: given the 600-digit product of two secret 300-digit primes, find the factors. Trial division has 1030010^{300} candidates to check. The best algorithm known to science, the general number field sieve , would need roughly 103510^{35} operations on a number that size (roughly 2,000 bits — the estimate is the number field sieve’s sub-exponential running-time formula evaluated there, about 21172^{117}), or five billion billion years at our one-op-per-nanosecond rate. The forward direction is homework; the reverse direction is the security assumption underneath TLS. Same numbers, same arithmetic, and an asymmetry of thirty orders of magnitude (103510^{35} operations against 9×1049 \times 10^4) that nobody has ever proven necessary.

Why decision problems — doesn’t “find the factors” ask for an answer, not a yes/no?

Complexity theory standardizes on decision problems — questions with yes/no answers, like “does NN have a prime factor smaller than kk?” — because they’re the cleanest to compare, and because almost nothing is lost. With that yes/no question answered for varying kk, binary search recovers the smallest factor, and repetition recovers the whole factorization — all with polynomially many calls. The general trick (using a decision oracle to construct a solution) is called self-reducibility, and Chapter 3 will run it with real numbers on this book’s running example. A set of inputs whose answer is “yes” is called a language, which is the word you’ll see in the literature; “decide the language” means “compute the yes/no function.”

The class P: a definition of feasible

P (polynomial time) is the class of decision problems — yes/no questions, per the accordion above — solvable by an algorithm whose running time is bounded by some polynomial:

P:T(n)≤c⋅nk\tip{the class of feasibly solvable problems}{\mathbf{P}} : \tip{running time at input size n}{T(n)} \le \tip{some fixed constant multiplier}{c} \cdot n^{\tip{some fixed exponent}{k}}

for some constants cc and kk that may depend on the problem but not on the input. Multiplication is in P (n2n^2 by grade school; nlog⁡nn \log n by the current record ). Sorting, shortest paths, matrix inversion, linear programming, primality testing (a famous late arrival — Chapter 3 tells that story): all in P.

Identifying “polynomial” with “feasible” is called the Cobham–Edmonds thesis , after the two people who proposed it independently around 1965. Jack Edmonds’ version, in his paper Paths, Trees, and Flowers , is worth reading in the original: he calls polynomial-time procedures “good algorithms,” and defends the definition against exactly the objection you’re forming. Why should n100n^{100} count as feasible and 2n/1002^{n/100} as infeasible, when for realistic nn the latter is faster? Three answers, in increasing order of importance:

  • Robustness. Polynomials are what survive the machine club’s simulation overheads. “Runs in n2n^2” is a statement about your computer; “runs in polynomial time” is a statement about the problem.
  • Composition. Polynomials are closed under the things programmers do: call a polynomial subroutine polynomially many times, feed one polynomial algorithm’s output (itself at most polynomially large) into another — the result is still polynomial. Feasibility that didn’t survive function composition would be useless as a theory.
  • Empirics. The thesis could have failed in practice, and mostly hasn’t: natural problems in P overwhelmingly turn out to have small exponents (n3n^3 and better) once anyone cares enough to optimize. Exponents like 100 show up in first proofs, then collapse. There are real exceptions (“galactic algorithms ” whose polynomial superiority only bites at absurd nn — the nlog⁡nn \log n multiplication above is one), and a fair scorecard records them. But the pattern is strong enough to bet on, and the field bet on it.

The right mental model for P is not “fast” but closed under being an engineer: within P, effort buys progress (profile, restructure, divide-and-conquer, buy a bigger machine) and the problem yields. Outside it, as the chart above showed, effort is a rounding error on the exponent.

Doesn’t randomness change the picture — half my favorite algorithms flip coins?

There’s a class for that: BPP, problems solvable in polynomial time by algorithms allowed to flip coins and be wrong with probability at most ⅓ (repetition drives the error below any threshold you like — kk repetitions shrink it exponentially in kk). For decades randomness looked like genuine extra power — primality testing was the poster child, solvable fast with coins before anyone could do it without. Then AKS  derandomized primality in 2002, and the modern consensus, for deep reasons connected to Chapter 12’s pseudorandomness, is that P = BPP: randomness buys convenience rather than power. This book will mostly say “P” and mean “P, and we’re not worried about coins.” Quantum (BQP) is the exception that genuinely might not collapse, and it waits for Chapter 3.

What we can already prove — and the exact spot where we can’t

A reasonable fear at this point: maybe questions like “does this problem require exponential time” are simply beyond mathematics — maybe nothing of the kind has ever been proven. The fear is wrong, and seeing why sharpens where the real difficulty lives.

In 1965, Juris Hartmanis and Richard Stearns proved the time hierarchy theorem : given meaningfully more time, a Turing machine can decide strictly more problems. There are problems solvable in time n3n^3 that provably cannot be solved in time n2n^2; problems solvable in 2n2^n that provably cannot be solved in any polynomial. In particular:

P⊊EXP\mathbf{P} \subsetneq \tip{problems solvable in exponential time}{\mathbf{EXP}}

— a genuine, unconditional separation, proven by a technique called diagonalization: roughly, build a problem whose answers are defined to disagree with every fast machine, one machine at a time. (The technique’s anatomy, its successes, and the precise reason it cannot settle P vs NP get a full chapter — Chapter 11.) So complexity theory can absolutely prove impossibility results.

The hierarchy theorem, though, separates classes defined by the same resource at different scales — time n2n^2 from time n3n^3, polynomial from exponential. Gödel’s question lives elsewhere. His proof-finding problem, the factoring asymmetry above, and the master problem of the next chapter all have the same two-sided shape: brute-force search solves them in exponential time (they sit inside EXP), and checking a proposed answer is easy (multiply the factors back; verify the proof line by line). The question is whether that checkability can be exploited to avoid the search. That question is about a different kind of resource than time-versus-more-time — and on it, the hierarchy theorem and its diagonalization are, as Chapter 11 will make precise, structurally silent.

Look again at what multiplication-vs-factoring actually shows: a problem where finding an answer seems to cost exponentially more than checking one. Checking a factorization: 90 microseconds. Finding it: five billion billion years, as far as anyone knows. Every hard problem you’ve ever cursed at has this flavor — the sudoku that’s obvious to verify, the proof that’s easy to referee. There is a name for the class of problems whose answers are easy to check (that class is NP — Chapter 2 explains the odd name), a single problem that turns out to sit at the top of all of them, and a 1971 theorem that earns it the rest of this book. That’s Chapter 2.

Further reading

Last updated on