
Algorithms and Turing Machines
Hilbert wanted a mechanical method to decide any statement of logic, and before anyone could prove there wasn't one, someone had to say exactly what a method is. Turing's 1936 answer, read from his paper: a clerk with squared paper reduced to a machine with a tape, a head, and a table of rules; his first example machine; a six-rule machine that adds one to a binary number, traced step by step; Church's lambda calculus and the Church-Turing thesis, stated carefully; and the universal machine, which reads another machine's description and runs it, the idea inside every computer since. Ends with a twenty-line Turing machine in Python.
- 12 min
- 9 steps
- 7 questions
- 6 hands-on
- Lesson 22 of 24 in How Computers Work
- Drafted by Opus 5.5
In this lesson
- Hilbert’s question
- A clerk with a pencil
- Turing’s machine
- A machine at work
- Church and the lambda calculus
- One machine for every program
- Run one yourself
- Turing on paper
- Teach it a new trick
Picking up where you left off.
Hilbert’s question
In 1928 David Hilbert and Wilhelm Ackermann called it “the main problem of mathematical logic”: find an effective method that decides, for any formula of first-order logic, whether it can be proved 1. The German name stuck: the Entscheidungsproblem, the decision problem. A decision method would answer “provable” for a formula like A implies A and “not provable” for A and not-A, mechanically, for every formula there is. Hilbert already had such methods for smaller logics: with Paul Bernays for propositional logic, and from Leopold Löwenheim’s work for logic with one-place predicates only. He expected one for the whole 1. Jacques Herbrand called it “the most general problem of mathematics” 1. In 1930 Hilbert rejected the idea of unsolvable problems outright: “We must know. We shall know.” 2

To prove that no such method exists, you first have to say exactly what a method is, and nobody had. In 1936 two people did, separately: Alonzo Church and Alan Turing. Both concluded that the Entscheidungsproblem has no solution; Church published a few months first 1. Turing’s paper reached the London Mathematical Society on 28 May 1936 and was read there on 12 November 3.
A clerk with a pencil
Turing began with a person working a calculation. “Computing is normally done by writing certain symbols on paper,” he wrote, and we may suppose the paper “divided into squares like a child’s arithmetic book” 3. Two dimensions are never essential, so make it a strip of squares, a tape. Allow only finitely many symbols. At each moment the person’s next step is fixed by the symbols they are looking at and their “state of mind”, of which there are finitely many, because memory is limited 3. Take away the person and what is left is a machine.

Turing’s machine
The machine has a finite number of conditions, which Turing called m-configurations. A tape divided into squares runs through it, and at any moment it scans exactly one square, “the only one of which the machine is, so to speak, ‘directly aware’” 3. Its configuration and the scanned symbol fix what it does next: write or erase a symbol, move one square left or right, and change its m-configuration 3. Today the m-configuration is called the state, and each rule reads: in this state, seeing this symbol, write that, move, and go to that state.
Turing’s first example prints 0 1 0 1 … forever, on every other square, starting from a blank tape 3:
| State | Scanned | Do | Next |
|---|---|---|---|
| b | blank | print 0, move right | c |
| c | blank | move right | e |
| e | blank | print 1, move right | f |
| f | blank | move right | b |
He wrote the states in Gothic letters; the blank squares in between were kept for rough working.
Two definitions in the paper matter for the next lesson. Turing’s machines were meant to compute numbers, printing binary digits forever: a machine that keeps printing them is circle-free, and one that gets stuck, or goes on moving without ever printing another digit, is circular 3. A number is computable if a circle-free machine prints its digits. That includes π, e, and the real parts of all algebraic numbers. Yet the machines can be listed one by one, while the real numbers cannot, so almost every real number is beyond any machine 3.
Put in order
This exercise needs JavaScript.
The state and the scanned symbol are all the machine knows; together they pick exactly one rule, and the rule says what to write, where to move, and what to remember.
A machine at work
Six rules add one to a binary number. In state right, walk right over the digits until a blank, then step back and switch to carry. In carry, a 1 becomes 0 and the carry moves left; a 0, or a blank past the left end, becomes 1, and the machine halts. It is the carry chain from Adding with Gates, done by hand.
Nothing in the machine knows what a number is. It reads, writes, steps, and changes state, and 1011 becomes 1100.
Work it out
This exercise needs JavaScript.
Three moves right across the digits and a fourth onto the blank; one move back in state carry; three carries turning 1s into 0s, the last stepping off the left end; and one move writing the new leading 1. The tape reads 1000, eight.
Fill the blanks
This exercise needs JavaScript.
It is the carry chain done by hand: 1 plus a carry is 0 with the carry moving left, and 0 or a blank plus a carry is 1, which ends the sum.
Church and the lambda calculus
Church came at it from logic. He identified the effectively calculable functions with the λ-definable ones, and equally with the recursive ones; he and Kleene proved those two classes the same 1. The lambda calculus is a bare notation for functions: λx[M] makes a function out of an expression M, and applying it to an argument substitutes the argument for x 4. Numbers can be written as lambda terms, and every computable function on them can be expressed 4. When Turing read Church’s paper, he added an appendix proving that λ-definability and his computability pick out the same functions 3 1.
That convergence is the main evidence for the Church–Turing thesis: every effective method, anything a person could work out by rote with pencil and paper, given unlimited time and paper, can be carried out by a Turing machine 1. It is a thesis, not a theorem, because “effective method” is an informal idea. And it is about clerks, not about every conceivable machine. The claim that whatever any machine can calculate a Turing machine can too, which Robin Gandy called Thesis M, is a separate and stronger claim, false for some idealized machines 1.
Match up
This exercise needs JavaScript.
Hilbert posed the decision problem and expected a solution; Church and Turing each defined computation precisely and showed there was none; Gandy separated their thesis about clerks from claims about all machines.
Quick check
It is about methods a person could follow by rote with paper and pencil. Claims about all possible machines are a different thesis, and the next lesson shows many problems have no algorithm at all.
One machine for every program
Turing wrote each machine’s table as a string of letters, then turned the letters into digits, giving each machine a description number. His first example’s is 31332531173113353111731113322531111731111335317 3. Then the decisive step: “It is possible to invent a single machine which can be used to compute any computable sequence.” Write another machine’s description on its tape, and this universal machine does what that machine would do 3. It works by writing out, square by square, each complete configuration the other machine would pass through, and Turing gave its full table of rules in the following section 3. Nothing about the symbols is special either: Claude Shannon proved in 1956 that any Turing machine can be reduced to one using only 0 and 1 2.
The program has become data on the tape. The universal machine, in the encyclopedia’s words, holds “the fundamental logical principles of the stored-program all-purpose digital computer” 1: the CPU in Instructions and the Fetch-Execute Cycle fetches its instructions from the same memory that holds its data. So one computer can run any program, and a laptop and a Turing machine with enough tape compute exactly the same things; one is billions of times faster 2. Donald Davies, who named the packet in Layers and Packets, joined the National Physical Laboratory in 1947 to work under Turing on the Automatic Computing Engine 5.

Sort
This exercise needs JavaScript.
Turing showed pi computable and built the universal machine in the same paper, and also proved the last two impossible; the next lesson takes the proof apart.
Find the mistake
This exercise needs JavaScript.
Church’s paper came out a few months before Turing’s. Shannon’s 1956 result is that two symbols, 0 and 1, are enough for any Turing machine.
Run one yourself
A Turing machine fits in twenty lines of Python. The tape is a dictionary from square numbers to symbols, so it can grow either way; _ is a blank. Save as tm.py:
# tm.py: a Turing machine. Each rule maps (state, symbol read) to (symbol to write, move, next state).
RULES = {
("right", "0"): ("0", +1, "right"), # walk right over the number
("right", "1"): ("1", +1, "right"),
("right", "_"): ("_", -1, "carry"), # past the end: step back onto the last digit
("carry", "1"): ("0", -1, "carry"), # 1 + carry = 0, carry 1 to the left
("carry", "0"): ("1", 0, "halt"), # 0 + carry = 1, finished
("carry", "_"): ("1", 0, "halt"), # ran off the left end: new leading 1
}
def show(step, state, tape, head):
squares = range(min(min(tape), head), max(max(tape), head) + 1)
cells = "".join(f"[{tape.get(i, '_')}]" if i == head else f" {tape.get(i, '_')} " for i in squares)
print(f"{step:>2} {state:<5} {cells}")
def run(word, state="right", head=0):
tape = dict(enumerate(word)) # square number -> symbol; unlisted squares are blank
step = 0
show(step, state, tape, head)
while state != "halt":
write, move, state = RULES[(state, tape.get(head, "_"))]
tape[head] = write
head, step = head + move, step + 1
show(step, state, tape, head)
return "".join(tape[i] for i in sorted(tape)).strip("_")
print("result:", run("1011"))
me@linuxbox:~$ python3 tm.py
0 right [1] 0 1 1
1 right 1 [0] 1 1
2 right 1 0 [1] 1
3 right 1 0 1 [1]
4 right 1 0 1 1 [_]
5 carry 1 0 1 [1] _
6 carry 1 0 [1] 0 _
7 carry 1 [0] 0 0 _
8 halt 1 [1] 0 0 _
result: 1100
Brackets mark the square under the head. The RULES dictionary is the whole machine; run is a tiny universal machine that will follow any table you give it. To try another machine, change RULES, the starting word, and the name of the halting state; nothing else needs to change. If the machine reaches a state and symbol with no rule, Python stops with a KeyError, which is exactly how the busy beavers in The Halting Problem halt.
Turing on paper
- “On Computable Numbers” itself, in the Proceedings of the London Mathematical Society 3. Sections 1 to 3 and section 9, the clerk with squared paper, need no logic; the scans of the journal pages are widely posted.
- The Stanford Encyclopedia of Philosophy has free entries on Turing machines and on the Church–Turing thesis, with the history of the Entscheidungsproblem 2 1.
Teach it a new trick
Add Turing’s own first machine to tm.py as a second rule table, with states b, c, e, f, and run it from an empty tape for twenty steps (add a step limit to the loop, since it never halts). Then write rules that subtract one from a binary number: the borrow runs the same way as the carry, with 0s turning into 1s until a 1 turns into 0. Test it on 1100 and on 1000.
Lesson complete
Nice work.
Teach it back
In two sentences, explain Algorithms and Turing Machines to a friend who missed it.
Related lessons
Among the starsLessons that point here
Sources for this lesson
- 1The Church-Turing Thesis. Stanford Encyclopedia of Philosophy. 2023. verifiedSubstantive revision December 18, 2023; read November 15, 2026. A common formulation: every effective computation can be carried out by a Turing machine; advanced independently by Church and Turing in the mid-1930s; the thesis concerns what a human can do calculating by rote with paper and pencil. The Entscheidungsproblem asks for an effective method to decide whether any given formula of a logical calculus is provable; Hilbert and Ackermann called it in 1928 'the main problem of mathematical logic'; both Church and Turing showed it unsolvable for the first-order functional calculus, publishing in 1936, Church a few months earlier. Church identified effective calculability with recursiveness and lambda-definability, classes proved identical by Church and Kleene; Turing, learning of Church's proposal while preparing his own paper for publication, quickly proved lambda-definability equivalent to his computability. Section 5.1: Gandy distinguished Turing's thesis from 'Thesis M', that whatever a machine can calculate a Turing machine can; Thesis M is false for some idealized machines; the 1936 thesis is about effective methods, not the limits of all 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 The Halting Problem.
- 3On 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. 230; p. 249; pp. 231, 249–250; p. 231; pp. 231–232; p. 233; pp. 232–233; pp. 230–231; p. 241; pp. 241–242.
Also cited in The Halting Problem.
- 4The Lambda Calculus. Stanford Encyclopedia of Philosophy. 2023. verifiedSubstantive revision July 25, 2023; read November 15, 2026. The lambda calculus is a simple notation for functions and application: forming functions by abstraction, written with lambda (Church 1932 wrote lambda x[M]), and applying them, with the beta rule turning (lambda x[M]) applied to x back into M; functions and arguments are on a par. Section 9.2: natural numbers can be represented as lambda terms, and all computable (number-theoretic) functions can be represented in the lambda calculus.
- 5Donald Davies. Encyclopaedia Britannica. verifiedRead November 15, 2026. Donald Watts Davies, born June 7, 1924, Treorchy, Wales, died May 28, 2000; inventor of packet switching, along with Paul Baran; from 1947 worked on the Automatic Computing Engine under Alan Turing at the National Physical Laboratory, Teddington, and stayed with NPL all his career; in 1965-66 devised packet switching, in which a data stream is broken into blocks, 'packets, as Davies called them', sent separately and reassembled; it was used in the late 1960s as the basis of ARPANET.
Also cited in Layers and Packets.