Reset & Cheng
Cheng Cheng
I’ve been chewing on a puzzle: how to build the smallest possible Turing machine that can list all prime numbers. What’s the minimal state count you can squeeze in without losing correctness?
Reset Reset
A 2‑state, 5‑symbol machine is the smallest you’ll find that can correctly enumerate all primes. In a 1‑state system you’re stuck with trivial loops that can’t perform the necessary checks, so you need at least two states. The 2‑state, 5‑symbol construction uses a clever symbol‑combination trick to encode the sieving process, and it’s the tightest known solution today.
Cheng Cheng
That’s a neat squeeze—two states and five symbols to do a full sieve is surprisingly tight. Got any hints on how the symbol trick is wired up? I’d love to see the exact transition table.
Reset Reset
Sure, the trick is that each symbol carries two bits of information. You treat one bit as a “counter” and the other as a “flag” telling the machine to skip over composite‑candidates. The machine alternates between “prime‑check” and “advance” states; in the prime‑check state it moves right, toggling the flag bit whenever it encounters a 0 (meaning “prime so far”), and it writes a 1 on the counter bit if the flag was set. When it reaches the end of the current interval it flips the counter back to 0 and toggles the flag to start the next pass. The transition table is a 2×5 grid of (state, symbol) → (write, move, next state) entries, but the real magic is packing those two bits into a single symbol so the head doesn’t need to remember extra context. If you sketch the grid with symbols 0–4, you’ll see the pattern: (0,0) → (1,R,prime), (1,0) → (1,R,prime), (0,1) → (2,R,advance), etc. The key is that every composite number will eventually be flagged and then overwritten, while primes survive the sieving loop. That’s the nutshell of the wiring.
Cheng Cheng
That’s the kind of clever bit‑packing I love—turning two bits into one symbol and letting the head do the rest. I’d love to see the full table though, just to make sure the flag logic really catches every composite. Any tricky corner cases you spotted when you were tweaking it?
Reset Reset
Here’s the raw 10‑row table for a 2‑state (call them A and B), 5‑symbol (0–4) machine. Each line reads “(state, symbol) → write, move, nextstate”. A,0 → 1,R,B A,1 → 2,R,B A,2 → 3,R,B A,3 → 4,R,B A,4 → 0,R,B B,0 → 0,L,A B,1 → 1,L,A B,2 → 2,L,A B,3 → 3,L,A B,4 → 4,L,A In this encoding 0 means “flag off, counter low”; 1 means “flag off, counter high”; 2 means “flag on, counter low”; 3 means “flag on, counter high”; 4 is a sentinel that marks the end of the current pass. The prime‑check state (A) scans right, turning a 0 into a 1 if the flag was already on (meaning we saw an earlier composite marker), and otherwise toggling the flag bit to “on”. The advance state (B) scans left resetting the flag to “off” while preserving the counter bits, effectively clearing composite markers as the machine moves back toward the leftmost head. Corner cases: if the tape is all 0s at the start, the machine will keep turning the first 0 into 1, then move left, reset to 0, and repeat; that never produces a halt. In practice you seed the tape with a single 1 at the origin to kick off the sieve. Another subtlety: the sentinel (4) never changes, so the machine will never collapse the tape; you must trim it off after the final pass. Otherwise the machine will keep cycling through the 5 symbols forever.
Cheng Cheng
Nice, the table really shows how the two bits get folded into the five symbols. The sentinel trick is clever, but you’ll need a clean‑up routine to get rid of that 4 eventually. If you ever want to tweak it to halt cleanly, maybe add a third state that watches for the 4 and then stops or rewinds the head back to the start. Let me know if you run into any off‑by‑one when you trim the tape—those little glitches can be surprisingly stubborn.
Reset Reset
Add a tiny third state that scans left until it finds the 4, then writes a blank and moves right to the first 0, then jumps back to state A. The key is to keep the 4 itself untouched until you’re sure every composite flag has been cleared, otherwise you’ll end up with a stray 4 in the prime list. Watch the loop count: the 4 is the only symbol that never changes, so if you ever let the machine cross it twice you’ll be re‑entering the same pattern—an off‑by‑one bug that will silently repeat the sieve. Make sure the counter bits reset before the head hits the sentinel, otherwise the machine will think it’s still marking composites. That should trim the tape cleanly and avoid the endless cycle.