Designing a Turing Machine to Compute the Sum of Two Positive Integers
We design a single-tape, deterministic Turing machine that computes the function for positive integers represented in unary.
We will use the common unary format:
- Input tape: (exactly one separator ).
- Output tape (upon halting): 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
, where is the blank symbol.
Transition function
, as in standard TM definitions.
Halting
We halt when we finish eliminating the and have shifted everything left, leaving only one block of ’s (with to the right).
Footnotes
-
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. ↩
-
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:
We repeatedly:
- Find the separator .
- Take the leftmost immediately to the right of that .
- “Move it left” by writing in the current spot and over the (equivalently: shift the entire right block one cell left).
- Continue until there is no to the right of the separator; then the separator becomes part of blanks and we halt.
This is essentially “remove the and shift left to fill the gap,” as described in the unary addition example.
Footnotes
-
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 1Scan to find the unique between the two unary blocks."
Left-shift the right block
Stage 2Repeatedly move the first after the into the position, thereby shifting all right-side ’s left by one."
Detect end of right block
Stage 3When the symbol right of the is blank, the shift is complete; halt with only ."
Formal transition-function construction (single-tape)
We define a TM with:
- States:
- Start state:
- Final state(s):
- Tape alphabet:
We will write transitions in the standard form: meaning: in state , scanning , write , move , and go to state .
Footnotes
-
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
- 1Step 1
Assume input is exactly with on the tape; head starts on the leftmost symbol of .
- 2Step 2
Move right until the unique is scanned; switch to the shifting routine with the head positioned on that .
- 3Step 3
While scanning a with a immediately to its right, swap the pair by writing on the and on the right cell, then return to the position to repeat.
- 4Step 4
When the symbol immediately right of is blank, replace with blank and halt; the tape now contains exactly followed by blanks.
Transition rules
Below, “scan blank” means the current tape cell contains .
1) Find the separator
This moves right across the left unary block until 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 , shift it left into the separator:
- \delta(q_{\text{shift}},1)=(q_{\text{shift_back}},0,L) But we currently only declared and , so we refine the state set slightly to keep the routine clean:
Let states be:
Then transitions are:
A. Find
B. If there is a to the right of , move it left by one cell Interpretation when in : the head is on the symbol right of the .
- ?
We need to write over the , not the . The clean way is:- In on a scanned , overwrite it with (mark the vacated position),
- move left to the cell,
- overwrite that with ,
- move right back to continue.
So:
C. Now we are on the cell; replace it by 1 and return right
D. If the right neighbor is blank, shifting is complete When the head in scans , it means there are no more ’s to move left. We then erase the separator by moving left once and writing blank, then halt.
So:
- is not enough because the head is on , not on . Instead:
- and:
Add one more state :
Full final set of transitions:
-
Find
-
Shift loop
-
Finish
Correctness reasoning:
- Each pass through swaps the separator with the leftmost immediately to its right, effectively shifting the entire right block one cell left.
- After such swaps, the right neighbor of the separator becomes blank, so the TM clears the remaining and halts.
- The left block length increases by exactly , yielding .
This matches the standard unary-addition “remove delimiter and shift left” method.
Footnotes
-
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, ). 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
In the unary model , what does the delimiter represent?