Designing a Turing Machine to Compute the Sum of Two Positive Integers

Designing a Turing Machine to Compute the Sum of Two Positive Integers

Verified Sources
Sep 12, 2026

We design a single-tape, deterministic Turing machine that computes the function f(m,n)=m+nf(m,n)=m+n for positive integers represented in unary.

We will use the common unary format:

  • Input tape: 1m  0  1n1^m\;0\;1^n (exactly one separator 00).
  • Output tape (upon halting): 1m+n1^{m+n} with no separator remaining (i.e., a single unary block).

This style of “unary addition by removing the separator and combining blocks” is standard in TM examples for unary addition. In particular, one approach is to repeatedly eliminate the delimiter and shift symbols to close the gap. The CS390-style TM example states that unary addition can be done by removing the separator and shifting remaining characters left to fill the gap.


Key modeling choices

Tape alphabet
Γ={1,0,\blank}\Gamma=\{1,0,\blank\}, where \blank\blank is the blank symbol.

Transition function
δ:(QF)×ΓQ×Γ×{L,R}\delta: (Q\setminus F)\times \Gamma \to Q\times \Gamma \times \{L,R\}, as in standard TM definitions.

Halting
We halt when we finish eliminating the 00 and have shifted everything left, leaving only one block of 11’s (with \blank\blank to the right).

Footnotes

  1. Turing Machines: Examples (CS390) — Unary Form Integer Addition (remove delimiter and shift left) https://www.cs.odu.edu/~zeil/cs390/f24/Public/turing-jflap/index.html - Provides the unary addition method: remove separator and shift remaining symbols left.

  2. Turing machine (Wikipedia) https://en.wikipedia.org/wiki/Turing_machine - Standard formal definition and transition function behavior (state, scanned symbol -> next state, written symbol, L/R; halting when undefined).

Turing Machine for Addition of Two Unary Numbers

High-level idea (close the gap)

Initial tape looks like:

11m  0  11n\blank\blank\underbrace{1\cdots 1}_{m}\;0\;\underbrace{1\cdots 1}_{n}\blank\blank\cdots

We repeatedly:

  1. Find the separator 00.
  2. Take the leftmost 11 immediately to the right of that 00.
  3. “Move it left” by writing 00 in the current spot and 11 over the 00 (equivalently: shift the entire right block one cell left).
  4. Continue until there is no 11 to the right of the separator; then the separator becomes part of blanks and we halt.

This is essentially “remove the 00 and shift left to fill the gap,” as described in the unary addition example.

Footnotes

  1. Turing Machines: Examples (CS390) — Unary Form Integer Addition (remove delimiter and shift left) https://www.cs.odu.edu/~zeil/cs390/f24/Public/turing-jflap/index.html - Provides the unary addition method: remove separator and shift remaining symbols left.

Conceptual stages of the unary-adder TM

Locate separator

Stage 1

Scan to find the unique 00 between the two unary blocks."

Left-shift the right block

Stage 2

Repeatedly move the first 11 after the 00 into the 00 position, thereby shifting all right-side 11’s left by one."

Detect end of right block

Stage 3

When the symbol right of the 00 is blank, the shift is complete; halt with only 1m+n1^{m+n}."

Formal transition-function construction (single-tape)

We define a TM MM with:

  • States: Q={qstart,qfind0,qshift,qhalt}Q=\{q_{\text{start}}, q_{\text{find0}}, q_{\text{shift}}, q_{\text{halt}}\}
  • Start state: q0=qstartq_0=q_{\text{start}}
  • Final state(s): F={qhalt}F=\{q_{\text{halt}}\}
  • Tape alphabet: Γ={1,0,\blank}\Gamma=\{1,0,\blank\}

We will write transitions in the standard form: δ(q,a)=(q,b,D)\delta(q,a)=(q',b,D) meaning: in state qq, scanning aa, write bb, move D{L,R}D\in\{L,R\}, and go to state qq'.

Footnotes

  1. Turing machine (Wikipedia) https://en.wikipedia.org/wiki/Turing_machine - Standard formal definition and transition function behavior (state, scanned symbol -> next state, written symbol, L/R; halting when undefined).

Step-by-step operational algorithm implemented by the TM

  1. 1
    Step 1

    Assume input is exactly 1m01n1^m0 1^n with m,n1m,n\ge 1 on the tape; head starts on the leftmost symbol of 1m1^m.

  2. 2
    Step 2

    Move right until the unique 00 is scanned; switch to the shifting routine with the head positioned on that 00.

  3. 3
    Step 3

    While scanning a 00 with a 11 immediately to its right, swap the pair by writing 11 on the 00 and 00 on the right cell, then return to the 00 position to repeat.

  4. 4
    Step 4

    When the symbol immediately right of 00 is blank, replace 00 with blank and halt; the tape now contains exactly 1m+n1^{m+n} followed by blanks.

Transition rules

Below, “scan blank” means the current tape cell contains \blank\blank.

1) Find the separator 00

  • δ(qstart,1)=(qstart,1,R)\delta(q_{\text{start}},1)=(q_{\text{start}},1,R)
  • δ(qstart,0)=(qshift,0,R)\delta(q_{\text{start}},0)=(q_{\text{shift}},0,R)

This moves right across the left unary block until 00 is found.

2) Shifting routine

We maintain the invariant: the head is positioned on the cell immediately right of the separator during the inner loop, so we can decide whether shifting is still possible.

  • If current cell is 11, shift it left into the separator:
    • \delta(q_{\text{shift}},1)=(q_{\text{shift_back}},0,L) But we currently only declared qshiftq_{\text{shift}} and qhaltq_{\text{halt}}, so we refine the state set slightly to keep the routine clean:

Let states be: Q={qstart,qfind0,qshift,qback,qhalt}Q=\{q_{\text{start}}, q_{\text{find0}}, q_{\text{shift}}, q_{\text{back}}, q_{\text{halt}}\}

Then transitions are:

A. Find 00

  • δ(qstart,1)=(qstart,1,R)\delta(q_{\text{start}},1)=(q_{\text{start}},1,R)
  • δ(qstart,0)=(qshift,0,R)\delta(q_{\text{start}},0)=(q_{\text{shift}},0,R)

B. If there is a 11 to the right of 00, move it left by one cell Interpretation when in qshiftq_{\text{shift}}: the head is on the symbol right of the 00.

  • δ(qshift,1)=(qback,1,L)\delta(q_{\text{shift}},1)=(q_{\text{back}},1,L)?
    We need to write over the 00, not the 11. The clean way is:
    1. In qshiftq_{\text{shift}} on a scanned 11, overwrite it with 00 (mark the vacated position),
    2. move left to the 00 cell,
    3. overwrite that 00 with 11,
    4. move right back to continue.

So:

  • δ(qshift,1)=(qback,0,L)\delta(q_{\text{shift}},1)=(q_{\text{back}},0,L)

C. Now we are on the 00 cell; replace it by 1 and return right

  • δ(qback,0)=(qshift,1,R)\delta(q_{\text{back}},0)=(q_{\text{shift}},1,R)

D. If the right neighbor is blank, shifting is complete When the head in qshiftq_{\text{shift}} scans \blank\blank, it means there are no more 11’s to move left. We then erase the separator 00 by moving left once and writing blank, then halt.

So:

  • δ(qshift,\blank)=(qhalt,\blank,L)\delta(q_{\text{shift}},\blank)=(q_{\text{halt}},\blank,L) is not enough because the head is on \blank\blank, not on 00. Instead:
  • δ(qshift,\blank)=(qclear,\blank,L)\delta(q_{\text{shift}},\blank)=(q_{\text{clear}},\blank,L) and:
  • δ(qclear,0)=(qhalt,\blank,R)\delta(q_{\text{clear}},0)=(q_{\text{halt}},\blank,R)

Add one more state qclearq_{\text{clear}}: Q={qstart,qshift,qback,qclear,qhalt}Q=\{q_{\text{start}}, q_{\text{shift}}, q_{\text{back}}, q_{\text{clear}}, q_{\text{halt}}\}

Full final set of transitions:

  1. Find 00

    • δ(qstart,1)=(qstart,1,R)\delta(q_{\text{start}},1)=(q_{\text{start}},1,R)
    • δ(qstart,0)=(qshift,0,R)\delta(q_{\text{start}},0)=(q_{\text{shift}},0,R)
  2. Shift loop

    • δ(qshift,1)=(qback,0,L)\delta(q_{\text{shift}},1)=(q_{\text{back}},0,L)
    • δ(qback,0)=(qshift,1,R)\delta(q_{\text{back}},0)=(q_{\text{shift}},1,R)
  3. Finish

    • δ(qshift,\blank)=(qclear,\blank,L)\delta(q_{\text{shift}},\blank)=(q_{\text{clear}},\blank,L)
    • δ(qclear,0)=(qhalt,\blank,R)\delta(q_{\text{clear}},0)=(q_{\text{halt}},\blank,R)

Correctness reasoning:

  • Each pass through (qshiftqback)(q_{\text{shift}}\to q_{\text{back}}) swaps the separator with the leftmost 11 immediately to its right, effectively shifting the entire right block one cell left.
  • After nn such swaps, the right neighbor of the separator becomes blank, so the TM clears the remaining 00 and halts.
  • The left block length increases by exactly nn, yielding 1m+n1^{m+n}.

This matches the standard unary-addition “remove delimiter and shift left” method.

Footnotes

  1. Turing Machines: Examples (CS390) — Unary Form Integer Addition (remove delimiter and shift left) https://www.cs.odu.edu/~zeil/cs390/f24/Public/turing-jflap/index.html - Provides the unary addition method: remove separator and shift remaining symbols left.

Visual intuition (state-logic diagram)

type="tip" title="Pro Tip" content="Unary addition on a single tape is often easiest when you choose a representation with a single delimiter (here, 00). Then the computation becomes a controlled tape-shift, which maps directly to head motion and local rewrites."

type="warning" title="Warning" content="Your transition rules must respect what symbol the head is scanning at each step. In particular, during shifting the machine alternates between being on the right-side symbol and the separator cell; mixing these up yields an incorrect swap."

Correctness & edge cases

Knowledge Check

Question 1 of 4
Q1Single choice

In the unary model 1m01n1^m0 1^n, what does the delimiter 00 represent?