Counter machines
A few bags of pebbles. Add one, take one away, check if a bag is empty. That is enough to compute anything.
●●○ moderate · where to see it
The idea
A counter machine (or register machine) has a handful of counters, each holding a natural number of any size. It can only do three things to them.
Read the story
- INC r adds 1 to counter r;
- DEC r takes 1 from it;
- JZ r, L jumps to line L if counter r is zero.
That looks too weak to do anything. Marvin Minsky showed in 1961 that it is not. Such a machine is universal, and two counters are enough. His book Computation: Finite and Infinite Machines (1967) made the result widely known.
The catch is that two counters are only universal with a heavy encoding. Many esolangs live inside that encoding.
How it works
Start with A = 3, B = 2.
Adding two counters
0: JZ A, 3 if A is empty, we are done
1: DEC A
2: INC B; goto 0
3: HALT
Start with A = 3, B = 2. The loop moves pebbles one at a time, (3,2) → (2,3) → (1,4) → (0,5), then halts. B ends at 5 after 10 instructions.
This is the same loop as Brainfuck's [->+<]. A Brainfuck cell is a counter; [ is JZ.
Why two counters are enough
A Turing machine's tape can be written as numbers. Take a machine with counters a, b, c. Pack them into one number, n = 2^a · 3^b · 5^c. Then each counter step is arithmetic on n.
- "add 1 to b" is "multiply n by 3";
- "take 1 from a" is "divide n by 2";
- "is c zero?" is "is n not divisible by 5?"
Multiplying and dividing by a constant needs a second counter as scratch space, moving pebbles back and forth. So any number of counters fold into two. It works, but it is exponentially slow.
FRACTRAN does exactly this
John Conway's FRACTRAN (1987) drops the second counter and the labels. A program is a list of fractions. Starting from a number n, repeatedly multiply n by the first fraction that gives a whole number. Stop when none does.
The one-fraction program 3/2 adds; given 2^a · 3^b, it ends at 3^(a+b).
72 = 2^3 · 3^2 → 108 → 162 → 243 = 3^5
Each step trades a factor 2 for a factor 3, moving one pebble from bag "2" to bag "3".
Seen in
- FRACTRAN. A counter machine written as fractions; the prime factors of one number are the counters.
- Brainfuck. Each cell is a counter. Three unbounded cells are enough to keep it universal (esolangs.org).
- Deadfish. One counter and no test-and-jump. The instructions are there, the loop is not, so it is not universal.
Try this
- Write a counter-machine program that copies A into B without emptying A. (You need a third counter.)
- Write a FRACTRAN program that turns 2^a · 3^b into 5^(a+b). (Two fractions.)
- Hard: with only the packed number n = 2^a · 3^b in one counter, try to do "multiply n by 3". Why do you need a second counter as scratch space?
Further
- esolangs.org/wiki/Minsky_machine
- Marvin Minsky, Computation: Finite and Infinite Machines (1967).
- John Conway, "FRACTRAN: A Simple Universal Programming Language for Arithmetic" (1987).
- See also one instruction, Turing completeness, tag systems.