FRACTRAN
A program is a list of fractions. Multiply your number by the first one that keeps it whole. Repeat. John Conway made it list every prime.
John Conway, 1987 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 2 examples and 2 known programs · Hello World on Try It Online: differs · Test method
Run it
Example
Expected output
15625
Output
The idea
John Conway, of the Game of Life, published FRACTRAN in a 1987 paper whose title calls it "a simple universal programming language for arithmetic". It has no variables, no loops and no instructions. Only fractions.
Read the story
The secret is prime factorisation. Every whole number is a unique product of primes, like 2³ · 3² = 72. Read each prime's exponent as a register: 72 holds 3 in register "2" and 2 in register "3". Multiplying by 3/2 then means "take one from register 2, add one to register 3". A fraction's denominator is a test (are these registers big enough?), and its numerator is an action.
So FRACTRAN is a counter machine in disguise. Counter machines are universal, so FRACTRAN is too. Conway's showpiece, PRIMEGAME, is 14 fractions that generate the primes in order.
The machine
One positive whole number n, and a list of fractions f₁, f₂, …, fₖ.
repeat:
find the first fᵢ in the list such that n × fᵢ is a whole number
if there is none, halt (n is the answer)
otherwise n = n × fᵢ
Order matters. Earlier fractions have priority, which is how FRACTRAN gets the effect of "if" and of program states. A prime that appears only as a flag (exponent 0 or 1) works like a line number.
Example
Addition: 3/2 alone.
Addition: 3/2 alone. Start with n = 2ᵃ · 3ᵇ. Each step moves one count from the 2s to the 3s, and it stops when no 2s are left. The answer is 3ᵃ⁺ᵇ.
72 = 2^3·3^2
108 = 2^2·3^3
162 = 2·3^4
243 = 3^5 3 + 2 = 5
Multiplication uses six fractions, with the primes 7, 11 and 13 as helper registers and flags:
PRIMEGAME starts at 2 and never halts. Every power of 2 it passes through has a prime exponent, in order: 2², 2³, 2⁵, 2⁷, … (the first appears after 19 steps, the second after 69).
# 2 3 5 7 11 13 17 19 23 29
Checked examples
Example
Expected output
15625
Output
Quirks
- A run stops after 3,000,000 steps.
Try this
- Write a one-fraction program that turns 2ᵃ into 3²ᵃ (doubling).
- Use
-ton the multiplication program with n = 2² · 3² = 36 and find the loop in the trace. - Hard: subtraction. Turn 2ᵃ · 3ᵇ (with a ≥ b) into 5ᵃ⁻ᵇ.
Patterns it teaches
- Counter machines: registers as prime exponents; a fraction is "decrement these, increment those, if you can".
- Turing completeness: proved by compiling any counter machine to fractions. Universal with one number and a list of rules.
- Turing tarpit: trivially simple to define, and a hard puzzle to write anything in.
Further
- esolangs.org/wiki/Fractran
- John H. Conway, "FRACTRAN: A Simple Universal Programming Language for Arithmetic", in Open Problems in Communication and Computation (Springer, 1987).
- Other rule-based languages: Thue, ///.
See also
Also in Rewriting
Write your own in Run Convert a program from FRACTRAN
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: fractran.
Command line
shib run fractran -e '13/21, 385/13, 1/7, 3/11, 7/2, 1/3' --input '108'shib manual fractranSource
- Module
langs/classic.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org