1. Home
  2. Theory of Computation
  3. Turing Machine

Turing Machine

The simplest machine that can compute anything a computer can. Watch the head read, write and move along the tape to add numbers and check palindromes.

Interactive 3DIntermediate13 min readTOCUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A Turing machine is the mathematical model of every computer: an infinite tape, a head that reads and writes one cell, and a table of rules. Each rule says: in this state, reading this symbol → write, move left/right, change state. Press Run.

Pseudocode

  1. (right, 0) → write 0, R, right
  2. (right, 1) → write 1, R, right
  3. (right, ␣) → write ␣, L, carry
  4. (carry, 1) → write 0, L, carry
  5. (carry, 0) → write 1, L, done
  6. (carry, ␣) → write 1, L, done
Read this model as text · 11 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A Turing machine is the mathematical model of every computer: an infinite tape, a head that reads and writes one cell, and a table of rules. Each rule says: in this state, reading this symbol → write, move left/right, change state. Press Run.
  2. Walk to the right end, then add 1 like you do on paper: 1 + 1 = 0 carry 1, until a 0 (or blank) absorbs the carry. The head starts on the first symbol in state right.
  3. In state right reading 1: write 1, move right, go to right.
  4. In state right reading 0: write 0, move right, go to right.
  5. In state right reading 1: write 1, move right, go to right.
  6. In state right reading 1: write 1, move right, go to right.
  7. In state right reading ␣: write ␣ (blank), move left, go to carry.
  8. In state carry reading 1: write 0, move left, go to carry.
  9. In state carry reading 1: write 0, move left, go to carry.
  10. In state carry reading 0: write 1, move left, go to done.
  11. Halted. The tape now reads 1100 — 1011 + 1 in binary (11 + 1 = 12). Simple rules, yet any computer program can be simulated by a Turing machine.

Try this in the 3D model

  • Run Binary +1 on 1011, then on 111. What happens to the carry?
  • Run Unary addition on 111+11. How is the + sign removed?
  • Run Binary palindrome? on 1001 and on 1011. Count the trips back and forth.

Alan Turing’s machine (1936)

Before electronic computers existed, Alan Turing asked: what does it mean to compute? His answer was an imaginary machine so simple that it obviously can be built, yet able to carry out any algorithm.

A Turing machine has:

  1. an infinite tape divided into cells, each holding one symbol (or a blank ␣),
  2. a head that reads and writes one cell and moves left or right,
  3. a finite set of states, including halting states,
  4. a rule table: (current state, symbol read) → (symbol to write, move L/R, next state).

That’s all. In the 3D model, the diamond is the head (with its current state above it), and the code panel lists the rule table — the highlighted line is the rule being applied.

Example 1: binary +1

Rules:

State Read Write Move Next
right 0 / 1 same R right
right ␣ ␣ L carry
carry 1 0 L carry
carry 0 1 L done
carry ␣ 1 L done

Walk to the end, then add 1 exactly as you would on paper: 1 + 1 = 0 carry 1, until a 0 or blank absorbs the carry. 1011 (11) → 1100 (12).

Example 2: unary addition

Numbers are written in unary: 3 = 111. To compute 111+11, replace the + with a 1 and erase one 1 from the end → 11111 (5).

Example 3: is it a palindrome?

Erase the first symbol and remember it in the state (have0 / have1), run to the end, check that the last symbol matches, erase it, run back to the start — and repeat. If everything matched, accept. This takes O(n²) steps because the head travels back and forth.

Why Turing machines matter

  • Church–Turing thesis: anything computable by an algorithm can be computed by a Turing machine. Your laptop, a phone and a supercomputer are all “just” fast Turing machines (with finite tape).
  • Universal Turing machine: one Turing machine can simulate any other, given its rule table on the tape — the idea behind stored-program computers.
  • Limits of computation: some problems can’t be solved by any algorithm. The famous halting problem — “will this program ever stop?” — is undecidable.

The halting problem in one paragraph

Suppose a program halts(P, x) could always answer correctly. Build trouble(P): if halts(P, P) says yes, loop forever; otherwise stop. Now ask: does trouble(trouble) halt? Either answer contradicts itself — so halts cannot exist.

Code: a tiny Turing machine simulator

def run_tm(rules, tape, state, halt, max_steps=1000):
    tape = dict(enumerate(tape))           # sparse "infinite" tape
    head = 0
    for _ in range(max_steps):
        if state in halt:
            break
        sym = tape.get(head, "_")
        write, move, state = rules[(state, sym)]
        tape[head] = write
        head += 1 if move == "R" else -1
    cells = [tape[i] for i in range(min(tape), max(tape) + 1)]
    return "".join(cells).strip("_"), state

increment = {
    ("right", "0"): ("0", "R", "right"), ("right", "1"): ("1", "R", "right"),
    ("right", "_"): ("_", "L", "carry"),
    ("carry", "1"): ("0", "L", "carry"), ("carry", "0"): ("1", "L", "done"),
    ("carry", "_"): ("1", "L", "done"),
}
print(run_tm(increment, "1011", "right", {"done"}))   # ('1100', 'done')

Common mistakes

  • Forgetting a rule for some (state, symbol) pair — the machine then halts unexpectedly.
  • Moving the head off the written part and forgetting that the tape is full of blanks there.
  • Thinking a Turing machine is a real device — it’s a mathematical model (although people have built physical ones for fun).

Complexity at a glance

Case / operationTimeWhy
Binary increment (n bits)O(n)
Palindrome check (n symbols)O(n²)The head runs back and forth n/2 times.
Extra spaceThe tape (unbounded)

Quick check

Test yourself — pick an answer to see if you got it.

1. What does a Turing machine rule specify?

2. What makes a Turing machine more powerful than a pushdown automaton?

3. What does the Church–Turing thesis say?

4. What is the halting problem?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Turing Machine. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.