I've tried to google for explanation but most links only say things like "FRACTRAN is turing complete. As an example, let's look at multiplication."
I remember seeing an xkcd forum post say that FRACTRAN helped the poster understand Turing Completeness. I am looking for an intuitive explanation as to why this esolang is Turing complete, as it's not very obvious looking at the language mechanics.
Best Answer
For an imperative language to be Turing complete it must have:
FRACTRAN is a language that is made up of a series of fractions that stores its data in the exponents of primes.
Lets say you want to add two numbers: 2a3b becomes 5ab
That's a FRACTRAN program for doing that above change.
You start off with a number like 72 (2332). The program goes 'forward' until it finds a number that when multiplied by the instruction is another integer (no remainders allowed).
72
will run forward until it gets to11/2
. It then will divide the number by2
and multiply it by11
(the power in 11 is a variable). This gives396
.396
is divisible by 33 (reducing the 3 power and the 11) and multiply by 455 (incrementing 5, 7 and 13 variables). And so on. The full description of this program and its state table can be read at FRACTRAN wikipedia page, including a really nice animated gif of the above program.Other FRACTRAN material on Stack Exchange that touches on the Turing completeness can be found at: Convert Fractran into Brainfuck (ok, that's a really productive use of one's time)
Part of the trick here (and this starts straying into theory) is that behind the scenes, this is a Minsky register machine for which it was proved that certain tapes (programs) are Turing machines IF the tape is represented as a Gödel number which is exactly what the FRACTRAN number is (from the linked wikipedia page):
So, we've got conditional loops, arbitrary variables stored as Gödel numbers, we've got a Turing machine.
Some other fun reading that touches on the Collatz like nature of FRACTRAN can be read at Can’t Decide? Undecide! that relate the Collatz conjecture to FRACTRAN and the halting problem.
FRACTRAN is a bit difficult to get one's head around.
Consider the program something like:
In this, each block is of the form:
The first statement from the multiplication program above:
Would be written in this form as:
And thus you can clearly see the data storage and the looping constructs necessary for Turing completeness. Its very rudimentary, but it exists and runs as a simple register machine - but then thats all that you really need to be able to do.
Still not convinced?
This largely borrows from a lecture by Dimitri Hendricks on Models of Computation
This takes the very simple program
(2/3)
which is an adder (2a3b --> 3a+b) But it is destructive - the value in 2 is cleared as part of the process.Lets write a higher level FRACTRAN that makes it easy to not do such destruction.
The original program could be thought of as:
In F2, one can specify 'functions' of a sort.
To convert an F2 program (P) into a standard FRACTRAN program, one does:
becomes:
What this has done is used the primes p, q, r, and s to store the state of the program.
And then we've got the register machine... it has a finite number of registers that store arbitrary large numbers and two instructions:
This register machine has been shown to be Turing complete.
It then goes to show the process over several slides of compiling a register machine program into a FRACTRAN program as part of a mechanical process.
Basically:
And thus because of the equivalence between these two models of computing, FRACTRAN is Turing complete.
Btw, if you really want to have your mind blown, read Code Golf: Fractran in which some people wrote a FRACTRAN program to run another FRACTRAN program.