FRACTRAN is a Turing-complete esoteric programming language invented by the mathematician John Conway. A FRACTRAN program is an ordered list of positive fractions together with an initial positive integer input n. The program is run by updating the integer n as follows:
gives the following FRACTRAN program, called PRIMEGAME, which finds successive prime numbers:
Starting with n=2, this FRACTRAN program generates the following sequence of integers:
After 2, this sequence contains the following powers of 2:
The exponent part of these powers of two are primes, 2, 3, 5, etc.
A FRACTRAN program can be seen as a type of register machine where the registers are stored in prime exponents in the argument n.
Using Gödel numbering, a positive integer n can encode an arbitrary number of arbitrarily large positive integer variables.[1] The value of each variable is encoded as the exponent of a prime number in the prime factorization of the integer. For example, the integer
represents a register state in which one variable (which we will call v2) holds the value 2 and two other variables (v3 and v5) hold the value 1. All other variables hold the value 0.
A FRACTRAN program is an ordered list of positive fractions. Each fraction represents an instruction that tests one or more variables, represented by the prime factors of its denominator. For example:
tests v2 and v5. If
v2\ge2
v5\ge1
Since the FRACTRAN program is just a list of fractions, these test-decrement-increment instructions are the only allowed instructions in the FRACTRAN language. In addition the following restrictions apply:
The simplest FRACTRAN program is a single instruction such as
This program can be represented as a (very simple) algorithm as follows:
FRACTRAN instruction | Condition | Action | |||
---|---|---|---|---|---|
| v2 > 0 | Subtract 1 from v2 Add 1 to v3 | |||
v2 = 0 | Stop |
Given an initial input of the form
2a3b
2a-13b+1
2a-23b+2
a
3 | |
2 |
3a
We can create a "multiplier" by "looping" through the "adder". In order to do this we need to introduce states into our algorithm. This algorithm will take a number
2a3b
5ab
Current state | Condition | Action | Next state |
---|---|---|---|
A | v7 > 0 | Subtract 1 from v7 Add 1 to v3 | A |
v7 = 0 and v2 > 0 | Subtract 1 from v2 | B | |
v7 = 0 and v2 = 0 and v3 > 0 | Subtract 1 from v3 | A | |
v7 = 0 and v2 = 0 and v3 = 0 | Stop | ||
B | v3 > 0 | Subtract 1 from v3 Add 1 to v5 Add 1 to v7 | B |
v3 = 0 | None | A |
State B is a loop that adds v3 to v5 and also moves v3 to v7, and state A is an outer control loop that repeats the loop in state B v2 times. State A also restores the value of v3 from v7 after the loop in state B has completed.
We can implement states using new variables as state indicators. The state indicators for state B will be v11 and v13. Note that we require two state control indicators for one loop; a primary flag (v11) and a secondary flag (v13). Because each indicator is consumed whenever it is tested, we need a secondary indicator to say "continue in the current state"; this secondary indicator is swapped back to the primary indicator in the next instruction, and the loop continues.
Adding FRACTRAN state indicators and instructions to the multiplication algorithm table, we have:
FRACTRAN instruction | Current state | State indicators | Condition | Action | Next state | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
| A | None | v7 > 0 | Subtract 1 from v7 Add 1 to v3 | A | ||||||
| v7 = 0 and v2 > 0 | Subtract 1 from v2 | B | ||||||||
| v7 = 0 and v2 = 0 and v3 > 0 | Subtract 1 from v3 | A | ||||||||
v7 = 0 and v2 = 0 and v3 = 0 | Stop | ||||||||||
,
| B | v11, v13 | v3 > 0 | Subtract 1 from v3 Add 1 to v5 Add 1 to v7 | B | ||||||
| v3 = 0 | None | A |
When we write out the FRACTRAN instructions, we must put the state A instructions last, because state A has no state indicators - it is the default state if no state indicators are set. So as a FRACTRAN program, the multiplier becomes:
With input 2a3b this program produces output 5ab. [2]
In a similar way, we can create a FRACTRAN "subtractor", and repeated subtractions allow us to create a "quotient and remainder" algorithm as follows:
FRACTRAN instruction | Current state | State indicators | Condition | Action | Next state | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
,
| A | v11, v13 | v2 > 0 and v3 > 0 | Subtract 1 from v2 Subtract 1 from v3 Add 1 to v7 | A | ||||||
| v2 = 0 and v3 > 0 | Subtract 1 from v3 | X | ||||||||
| v3 = 0 | Add 1 to v5 | B | ||||||||
,
| B | v17, v19 | v7 > 0 | Subtract 1 from v7 Add 1 to v3 | B | ||||||
| v7 = 0 | None | A | ||||||||
| X | v3 > 0 | Subtract 1 from v3 | X | |||||||
v3 = 0 | Stop |
Writing out the FRACTRAN program, we have:
and input 2n3d11 produces output 5q7r where n = qd + r and 0 ≤ r < d.
Conway's prime generating algorithm above is essentially a quotient and remainder algorithm within two loops. Given input of the form
2n7m
For this program, reaching the prime number 2, 3, 5, 7... requires respectively 19, 69, 281, 710,... steps .
A variant of Conway's program also exists, which differs from the above version by two fractions:
This variant is a little faster: reaching 2, 3, 5, 7... takes it 19, 69, 280, 707... steps . A single iteration of this program, checking a particular number N for primeness, takes the following number of steps:where
b<N
\lfloorx\rfloor
In 1999, Devin Kilminster demonstrated a shorter, ten-instruction program: For the initial input n = 10 successive primes are generated by subsequent powers of 10.
The following FRACTRAN program:
calculates the Hamming weight H(a) of the binary expansion of a i.e. the number of 1s in the binary expansion of a.[3] Given input 2a, its output is 13H(a). The program can be analysed as follows:
FRACTRAN instruction | Current state | State indicators | Condition | Action | Next state | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
,
| A | v5, v11 | v2 > 1 | Subtract 2 from v2 Add 1 to v3 | A | ||||||
| v2 = 1 | Subtract 1 from v2 Add 1 to v13 | B | ||||||||
| v2 = 0 | None | B | ||||||||
| B | None | v3 > 0 | Subtract 1 from v3 Add 1 to v2 | B | ||||||
| v3 = 0 and v7 > 0 | Subtract 1 from v7 Add 1 to v2 | A | ||||||||
| v3 = 0 and v7 = 0 and v2 > 0 | Subtract 1 from v2 add 1 to v7 | B | ||||||||
v2 = 0 and v3 = 0 and v7 = 0 | Stop |