The Halting Problem
No program can look at every other program and say whether it will ever finish. Turing's own 1936 argument, which was about machines that stop printing digits, then the modern proof laid out step by step with a self-defeating program called contrary, and the diagonal table that gives the trick its name; Rice's theorem, which extends the result to nearly every question about what a program does; why no tool finds every bug and no antivirus catches every virus; and the busy beavers, where five states and 47,176,870 steps took until 2024 to settle, run here in Python.
- 12 min
- 9 steps
- 8 questions
- 6 hands-on
- Lesson 23 of 24 in How Computers Work
- Drafted by Opus 5.5
In this lesson
- Turing’s own version
- The modern proof
- Why it’s called diagonal
- Rice’s theorem
- What it costs
- Busy beavers
- Try it on Linux
- Further reading
- Beat the checker yourself
Picking up where you left off.
Turing’s own version
Algorithms and Turing Machines ended with Turing’s universal machine. In the same paper he turned it against itself. His machines were meant to print binary digits forever; a description number belonging to such a circle-free machine he called satisfactory, and he asked whether some machine D could test any number and say whether it is satisfactory 1.
Suppose D exists. Combine it with the universal machine into a new machine H that goes through the numbers 1, 2, 3, …, uses D to skip the unsatisfactory ones, runs each satisfactory machine, and prints the next digit of a long diagonal sequence: the first digit of the first good machine, the second of the second, and so on. H never gets stuck, so it is circle-free, and it has a description number K of its own. When H reaches K, it must test itself, find itself satisfactory, and then “calculate the first R(K) figures computed by H and write down the R(K)-th.” That figure would never be found; H would be stuck after all 1. Both verdicts are impossible, so there is no D. Turing went on to show that no machine can decide whether an arbitrary machine ever prints 0, and from that, that the Entscheidungsproblem has no solution 1 2.
The name halting problem isn’t Turing’s. It is a later version, close to his, that appears in Martin Davis’s 1958 textbook 2. “Turing proved the halting problem undecidable” is a convenient shorthand, not a quotation.
Quick check
His machines were meant to print digits forever; he proved no machine can tell the circle-free ones from the ones that get stuck, nor whether a machine ever prints 0. The halting problem is the later, closely related form.
The modern proof
The version programmers learn was popularized by Christopher Strachey in 1965 2. Suppose someone hands you a perfect checker:
halts(program, input)
-- always finishes, and returns True if program would finish on input,
-- False if it would run forever
contrary(program):
if halts(program, program):
loop forever
else:
stop
- A program is just text, so it can be given its own text as input.
contraryasks the checker what a program does when fed itself, and does the opposite.- Now run
contrary(contrary). - If
haltssays it finishes,contraryloops forever: the checker was wrong. - If
haltssays it runs forever,contrarystops at once: wrong again. - Every line except the first assumption is ordinary programming, so the assumption must be false: no
haltscan be right about every program 2.
Put in order
This exercise needs JavaScript.
Every step after the first is ordinary programming. The contradiction can only be blamed on the first assumption.
Why it’s called diagonal
Turing called his method “the diagonal process” 1. Picture every program run on every program as input:
contrary is built to differ from row 1 at column 1, from row 2 at column 2, and so on down the diagonal, so it can’t be any row in the table. But the table lists every program, and contrary is a program. The only escape is that the table can’t be filled in by any machine.
The trick is older than computers. It is the argument that shows the real numbers can’t be listed: list them, change the first digit of the first, the second digit of the second, and so on, and the number you build is on nobody’s line. Turing opens his section 8 by asking why the same argument doesn’t show that the computable numbers can’t be listed, when he had just listed them. His answer: building the diagonal number would require telling the circle-free machines from the rest, and that is exactly what no machine can do 1.
Rice’s theorem
In 1953 Henry Rice pushed the result as far as it goes. Take any property of what a program does, as opposed to how its text looks: if some programs have it and some don’t, then no algorithm can decide it for all programs 3. “Does it always return an answer?”, “Does it correctly add two numbers?” are both undecidable 3. So are “Does it ever print hello?” and “Is it the same as this other program?”, since each depends only on behavior. Questions about the text, such as its length or whether it contains a loop, are easy; the moment a question is about what the program will do when run, Rice’s theorem applies.
Sort
This exercise needs JavaScript.
Questions about the text can be checked by reading it. Questions about what the program does when run fall under Rice’s theorem.
Find the mistake
This exercise needs JavaScript.
Strachey popularized the modern proof; Turing’s own argument was about machines that stop printing digits. Rice’s theorem covers behavior: questions about the text, like its length, are easy.
What it costs
No tool can find every bug. “Can this program ever crash, leak the password, or divide by zero?” are questions about behavior, so any automatic checker must sometimes miss a real bug, raise a false alarm, or fail to answer 3. Real tools choose which of those to accept; none escapes all three.
Viruses are the same story. In 1984 Fred Cohen, in the paper that set out the computer virus as a security problem, showed that deciding whether a program is a virus is undecidable. His proof is a program called contradictory-virus, which asks the supposed detector about itself and infects other programs only if the detector says it is harmless 4. Detection is undecidable by inspecting the code beforehand or by watching it run 4.
It is also why a computer can’t warn you that a command will hang; you find out by waiting, then stop it by hand with the signals in Jobs and Signals. None of this says a particular program can’t be checked. The busy beaver hunters below settled the halting of 181 million particular machines. What fails is one method that works for every program.
Quick check
Cohen’s contradictory virus asks the detector about itself and infects only if the answer is no. That is the halting argument again, so perfect detection is undecidable, whatever the speed or the lists.
Busy beavers
In 1962 Tibor Radó turned the halting problem into a game. Among all Turing machines with n states and two symbols, started on a blank tape, find the one that runs longest and still halts. That longest run is S(n), and the most 1s left behind is Σ(n). If S were computable, so would be halting: to settle any n-state machine, run it for S(n) + 1 steps, and if it hasn’t stopped, it never will 5.
| States | S(n), the most steps | Settled |
|---|---|---|
| 1 | 1 | |
| 2 | 6 | |
| 3 | 21 | |
| 4 | 107 | 1983, by Allen Brady |
| 5 | 47,176,870 | 2024 |
| 6 | more than 2↑↑↑5 | open |
In 1963 Radó called the four-state case “entirely hopeless” 5. In 1983 a two-day competition at the University of Dortmund found a five-state machine that ran 134,467 steps; in 1989 Heiner Marxen and Jürgen Buntrock found one that runs 47,176,870. In 1990 Allen Brady predicted that nobody would ever prove it the winner 5.
In 2022 Tristan Stérin, a graduate student, opened the Busy Beaver Challenge website to anyone; more than 20 people joined, most without academic posts 6. In the spring of 2024 a contributor known as mxdys released a proof in the Coq proof assistant that settles every one of 181,385,789 five-state machines: S(5) = 47,176,870 and Σ(5) = 4,098, the first new value in over 40 years and the first ever checked by machine 5 6.
How do you settle 181 million machines when no general method exists? With many partial ones. A simple loop detector disposed of more than 175 million, catching both the machines that halt and those that fall into an obvious repeating cycle; cleverer deciders handled about 6 million more; 183 “long halters” were simply run until they stopped. Thirteen machines beat every decider and needed proofs of their own. One, called Skelet #1, wanders chaotically for more than 5.41 × 10⁵¹ steps before it settles into a loop it never leaves 5. That is Rice’s theorem in practice: each tool works on some machines, and the stubborn rest take a person.

Six states is another world. One six-state machine, Antihydra, halts only if a conjecture resembling the famous unsolved Collatz problem is false. A 25-state machine halts only if Goldbach’s conjecture is false, and a 748-state machine halts only if standard set theory is inconsistent, so S(748) can never be proved from those axioms 5.
Match up
This exercise needs JavaScript.
The first four were settled by 1983. The fifth took until 2024, a jump of more than 400,000 times.
Work it out
This exercise needs JavaScript.
None: it never halts. Any five-state machine that halts does so within S(5) steps, so knowing S(n) settles the halting of every n-state machine, which is why S can’t be computable.
Try it on Linux
The champion’s table, in the bbchallenge notation, lists for each state A to E what to do on 0 and then on 1; 1RZ writes 1, moves right, and enters Z, the halt 5. Save as bb5.py:
# bb5.py: run the five-state busy beaver champion until it halts.
# Each state A-E has a rule for reading 0 and one for reading 1: symbol to write, move, next state.
import time
TABLE = "1RB1LC_1RC1RB_1RD0LE_1LA1LD_1RZ0LA" # Z is the halt state
RULES = {}
for state, pair in zip("ABCDE", TABLE.split("_")):
for symbol, rule in zip((0, 1), (pair[:3], pair[3:])):
RULES[state, symbol] = (int(rule[0]), 1 if rule[1] == "R" else -1, rule[2])
tape = bytearray(30_000) # 30,000 squares, all 0
head, state, steps = 15_000, "A", 0
lo = hi = head
start = time.perf_counter()
while state != "Z":
write, move, state = RULES[state, tape[head]]
tape[head] = write
head += move
steps += 1
lo, hi = min(lo, head), max(hi, head)
print(f"halted after {steps:,} steps with {sum(tape):,} ones on the tape")
print(f"it used {hi - lo + 1:,} squares; {time.perf_counter() - start:.0f} seconds")
me@linuxbox:~$ python3 bb5.py
halted after 47,176,870 steps with 4,098 ones on the tape
it used 12,289 squares; 19 seconds
Five rules and twenty seconds of work. Knowing that no five-state machine does better took two years and a computer-checked proof.
Beat the checker yourself
Write halts in Python as a guess: run the program for a million steps and say False if it hasn’t finished. Then write contrary and feed it a few functions, and find a program that halts after more than a million steps, proving your checker wrong. Finally, change TABLE in bb5.py to the four-state winner, 1RB1LB_1LA0LC_1RZ1LD_1RD0RA 5, and confirm 107 steps and 13 ones.
Lesson complete
Nice work.
Teach it back
In two sentences, explain The Halting Problem to a friend who missed it.
Related lessons
Among the starsLessons that point here
Sources for this lesson
- 1On Computable Numbers, with an Application to the Entscheidungsproblem. Proceedings of the London Mathematical Society, series 2, 42: 230-265. 1936. verifiedRead November 15, 2026 in a scan of the journal pages (University of Virginia course copy). Received 28 May 1936, read 12 November 1936. A number is computable if its decimal can be written down by a machine. Section 1: a machine has finitely many conditions, m-configurations; a tape divided into squares, each able to bear a symbol; one scanned square at a time, the only one the machine is 'directly aware' of; it can write or erase the scanned symbol, move one square left or right, and change m-configuration; justified because human memory is necessarily limited. Section 3, example I: four m-configurations b, c, e, f print 0 and 1 on alternate squares (b: P0, R to c; c: R to e; e: P1, R to f; f: R to b). Section 5: tables written as standard descriptions and description numbers; machine I has description number 31332531173113353111731113322531111731111335317; computable sequences are enumerable. Section 6: 'It is possible to invent a single machine which can be used to compute any computable sequence' when given the standard description of the other machine. Section 8: a satisfactory number is the description number of a circle-free machine; suppose a machine D decides it; combine D with the universal machine into H, which works through 1, 2, 3, and writes the R(N)-th figure of each satisfactory machine's sequence; H is circle-free; but at its own number K it would have to 'calculate the first R(K) figures computed by H and write down the R(K)-th', which is never found, so H would be circular; both verdicts are impossible, so there is no D; likewise no machine E decides whether an arbitrary machine ever prints 0. Section 9: computing is done on paper 'divided into squares like a child's arithmetic book'; the one-dimensional tape, a finite number of symbols, and the computer's behaviour set by the symbols observed and the 'state of mind'. Section 11: the Hilbertian Entscheidungsproblem has no solution. An appendix outlines the equivalence of computability with Church's effective calculability. Cited at: p. 246; pp. 246–247; p. 248.
Also cited in Algorithms and Turing Machines.
- 2Turing Machines. Stanford Encyclopedia of Philosophy. 2025. verifiedSubstantive revision May 21, 2025; read November 15, 2026. Turing introduced his machines to prove there is no general method to decide every statement of first-order logic; the result went against Hilbert's hopes, quoting Hilbert (1930): instead of the 'stupid Ignorabimus', 'We must know. We shall know.' The universal machine computes what any other Turing machine computes, by interpreting its program and mimicking it, so program and behaviour become interchangeable. Section 2.4.3: Turing's own decision problems (CIRC?, PRINT?) concern whether a machine will fail to go on printing; the better-known halting problem, HALT?, whether a machine halts, is close to but not the same as Turing's, and is found in Davis 1958; the popular proof assumes a halting decider and derives a contradiction by running a machine that does the opposite on its own description; a popular, informal variant was given by Christopher Strachey in the context of programming (Strachey 1965). Post's halting state entered standard definitions through Kleene 1952 and Davis 1958.
Also cited in Algorithms and Turing Machines.
- 3Recursive Functions. Stanford Encyclopedia of Philosophy. 2024. verifiedSubstantive revision March 1, 2024; read November 15, 2026. Theorem 3.4 (Rice 1953): every non-trivial index set is undecidable, an index set being a set of programs closed under computing the same function, so membership depends only on what the program computes; Rice's theorem shows many decision problems of practical import undecidable, e.g. whether a program always returns an output or correctly computes a given function such as addition. Cites H. G. Rice, 'Classes of Recursively Enumerable Sets and Their Decision Problems', Transactions of the American Mathematical Society 74(2), 1953.
- 4Computer Viruses - Theory and Experiments. University of Michigan EECS 588 course handout of the 1984 paper. 1984. verifiedRead November 15, 2026. Defines the computer virus by its ability to attach itself to other programs and cause them to become viruses as well. Detection: deciding whether a program P is a virus is undecidable, since P could call the decision procedure D and infect other programs if and only if D says P is not a virus; 'program contradictory-virus' infects only if not D(contradictory-virus); a program that precisely tells a virus from any other program by examining its appearance is infeasible; detection is undecidable both a priori and at run time.
- 5Determination of the fifth Busy Beaver value. arXiv:2509.12337 (v2, 23 March 2026). 2025. verifiedRead November 15, 2026. S(n) is the most steps an n-state, 2-symbol Turing machine makes from an all-zero tape before halting; Radó introduced it in 1962 as one of the simplest uncomputable functions, with Sigma(n), the most 1s left on the tape; if S were computable it would decide the halting problem. Known before: S(1)=1, S(2)=6, S(3)=21, S(4)=107. A 1983 competition at the University of Dortmund found a 134,467-step machine (Uwe Schult); in 1989 Heiner Marxen and Jürgen Buntrock found the champion 1RB1LC_1RC1RB_1RD0LE_1LA1LD_---0LA, 47,176,870 steps; Aaronson conjectured in 2020 it was the winner. The paper proves S(5)=47,176,870 and Sigma(5)=4,098 in the Coq proof assistant (Coq-BB5, released in spring 2024 by contributor mxdys, 27,274 lines of Coq), deciding the halting of 181,385,789 enumerated machines; the first new Busy Beaver value in over 40 years and the first formally verified. Radó in 1963 called Sigma(4) and S(4) 'entirely hopeless'; Allen Brady predicted in 1990 that S(5)=47,176,870 would never be proved. S(6) > 2↑↑↑5; the 6-state Antihydra halts only if a Collatz-like conjecture fails; Goldbach's conjecture is encoded in a 25-state machine, the Riemann hypothesis in 744 states, and ZF's consistency in 748 (later 636), so S(748) cannot be proved in ZF. Halting convention: a machine halts on reaching an undefined transition, and the halting step counts; the original setup's halting transition writes a 1 (1RZ); Appendix B gives the winners, e.g. S(4)=107 by 1RB1LB_1LA0LC_1RZ1LD_1RD0RA (Sigma(4)=13, proved by Brady in 1983) and S(5) by 1RB1LC_1RC1RB_1RD0LE_1LA1LD_1RZ0LA. Table 3, the S(5) pipeline: loop detection decided 175,373,810 machines, n-gram closed position sets 6,005,142, other deciders a few thousand, 183 long halters were simulated, and 13 sporadic machines needed individual proofs, among them Skelet #1, which reaches an infinite loop only after more than 5.41 x 10^51 steps of chaos.
- 6With Fifth Busy Beaver, Researchers Approach Computation's Limits. Quanta Magazine. 2024. verifiedJuly 2, 2024; read November 15, 2026. Over 40 years ago computer scientists gathered in Dortmund to hunt busy beavers; two years before, graduate student Tristan Stérin launched the Busy Beaver Challenge website, open to all; the online community grew to more than 20 contributors around the world, most without traditional academic credentials; they proved the value of BB(5).