How Computers Work

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

  1. Hilbert’s question
  2. A clerk with a pencil
  3. Turing’s machine
  4. A machine at work
  5. Church and the lambda calculus
  6. One machine for every program
  7. Run one yourself
  8. Turing on paper
  9. Teach it a new trick

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

Black-and-white portrait of a bearded man in round wire spectacles, a wide-brimmed pale hat with a dark band, a dark jacket, and a high white collar with a patterned tie, looking at the camera.
David Hilbert, before 1912. He believed every mathematical problem could be solved, and asked for a method to decide them. Credit: Unknown photographer, via Wikimedia Commons · Public domain · Source

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.

Black-and-white studio portrait of a dark-haired man in a tweed jacket, striped shirt, and dark spotted tie, turned slightly to his right and looking past the camera.
Alan Turing in March 1951, fifteen years after the paper that defined computation. Credit: Elliott & Fry, via Wikimedia Commons · Public domain · Source

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

Put one move of a Turing machine in order.

This exercise needs JavaScript.

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.

Nine rows, steps 0 to 8, each showing a state badge and five tape squares with the scanned square outlined. Steps 0 to 4, state right: the tape reads 1 0 1 1 and a blank, and the head moves one square right each step, reading 1, 0, 1, 1, leaving each unchanged, until it sits on the blank. Step 5: reading the blank, it steps back onto the last 1 and switches to state carry. Step 6: reads 1, writes 0, moves left; tape 1 0 1 0. Step 7: reads 1, writes 0, moves left; tape 1 0 0 0. Step 8, state halt: reads 0, writes 1; tape 1 1 0 0. Footer: result 1100, twelve, after 8 moves; the same six rules add one to any binary number.
Eleven plus one, done with nothing but reading, writing, and stepping. Credit: StudyCorner original diagram, traced with the program in this lesson · CC BY 4.0 · Source

Nothing in the machine knows what a number is. It reads, writes, steps, and changes state, and 1011 becomes 1100.

Work it out

Run the six adding rules on the tape 111 (seven), starting on the first digit in state right. How many moves until it halts?

This exercise needs JavaScript.

Fill the blanks

Fill in the second half of the adding machine’s rules.

This exercise needs JavaScript.

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

Match each person to their part.

This exercise needs JavaScript.

Quick check

What does the Church-Turing thesis claim?

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.

A working model on a black base in a glass case: a long white paper tape runs from one large reel on the left, under a clear acrylic frame holding a small head with motors, wires, and brass knobs, to a second reel on the right.
A working Turing machine built by Mike Davey: a paper tape, a head that reads and writes it, and a small table of rules inside. Credit: Rocky Acosta, via Wikimedia Commons · CC BY 3.0 · Source

Sort

Given unlimited time and tape, which can a Turing machine do?

This exercise needs JavaScript.

Find the mistake

Two things in this account of who did what are wrong.

This exercise needs JavaScript.

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.

1day streak
0/1today's goal
–correct

Teach it back

In two sentences, explain Algorithms and Turing Machines to a friend who missed it.

Up next · 12 min

The Halting Problem

Could someone write a perfect checker that reads any program, with its input, and says whether it will ever finish?

Next lesson

Related lessons

Among the stars
Sources for this lesson
  1. 1
    The 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.
  2. 2
    Turing 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.

  3. 3
    On 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.

  4. 4
    The 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.
  5. 5
    Donald 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.