# Fractran A Simple Programming Language

## Introduction

Today, I want to talk about FRACTRAN, a simple (though not easy) programming language. FRACTRAN was invented by the great John Conway which is mostly known for his puzzles and his recreational math. I first encountered FRACTRAN as a teenager in the book “Ancient puzzles: Classic brainteasers and other timeless mathematical games of the last 10 centuries” by Dominic Olivastro. Actually I read the german version “Das chinesische Dreieck”, since I was a teenager and at that time read most books in my native language. Olivastro explained a very simple model of computation and dumbfounded me with the PRIMEGAME example, a program in FRACTRAN which can compute primes. I did not know how it works and it was a mystery to me. Nevertheless I found it very captivating and it put me under its spell. So I revisited it and wrote a blog post with the intention to document how I lifted the mysteries from FRACTRAN.

If you are interested in the original sources, you may probably want to check “FRACTRAN: A simple universal programming language for arithmetic” by John Conway in the journal Open Problems in Communication and Computation.

## How FRACTRAN works

A FRACTRAN program is a finite list of fractions

Bamm! That is all the syntax you need to know for this article. Forget specs of thousands of pages like in Java. Just lists of fractions.

So, how do we evaluate such a program? You simply give it as input a natural number and it gives you back an output computed as follows: We define as , where is the smallest index such that is an integer. If there is no , such that is an integer we return .

## Examples

### Moving a value

Let us take a look at the following program

On input for some , it tests if is an integer and if yes, replaces the current state with . It then repeats, until we cannot get integers anymore. The result is then .

### PRIMEGAME

This is too early to understand the PRIMEGAME, but i think it is nice to introduce it here, since it played such an important role for my fascination with FRACTRAN.

Consider the program

When given 2 as input it outputs an endless series which contains only prime powers of 2. I.e. . If you are looking for a nice nerdy tattoo and do not want to have the graph of a sine or the euler product you may want to consider this sequence of fractions.

## More verbose explanation

FRACTRAN is basically a register machine, where the values of multiple registers are stored in the valuations (exponents) of the primes. The value of the first register is the power of 2. The value of the second register is the power of 3, and so on. More concretely, the number corresponds to three registers with values . Each fraction, i.e., each instruction tests if the registers corresponding to the prime factors of are set. If that is the case, these registers are decremented and the registers corresponding to the prime factors of are incremented.

This model of computation is turing-complete, which means that any computable function can be computed by a FRACTRAN program. If you do not believe me, check out the original article of Conway, where a FRACTRAN program which enumerates all computable functions is given. Another way of recognizing turing-completeness is if you are familiar with register machines, especially Minksy machines.

## Doing interesting stuff

We have already seen how to move a value from one register to another. Next, we want to do more interesting stuff. To this end I have written a FRACTRAN interpreter in Python 3.

### Copying a value

We want to input and have as output . Unfortunately this does not work. Since the first input is only divisible by powers of two, there needs to be a fraction with two in the denominator (otherwise no instruction is executed). However, this instruction would also be carried out on the output.

That means we need to set some control registers. As input we take for some prime p and as output we want to have . The prime indicates our state. Since checking for a state is basically division, the register is decremented when we check. Thus we need two state bits, which we use alternately. Since is also decremented, we need first to copy the value to another register.

Let us try it step by step. The program yields given input . To copy the value of the -register back, we need to change the state. Marking the first state with the two primes and we get the program . If we evaluate this on we receive . But now it is trivial to move the value back from the -register.

The final program which outputs given input is thus

Let us check:

### Addition

Actually, addition is the same as our copy command, when we do not have a clean target register. If we evaluate the program on input we copy the on top of and get .

### Subtraction

After tackling addition, the obvious next trivial operation is subtraction. If we have we want to feed our program and it should return . This can be achieved with the program . Note that if we get instead . Thus we can also use this to represent negative numbers.

### Fibonacci numbers

Let us write a nontrivial program. We want to compute Fibonacci numbers. Let denote the -th Fibonacci number. We want to have a program, that given input returns . The in the input is again for maintaining state.

First we copy the powers of to so that we have . This can be achieved by the fraction .

Next, we copy the powers of back to and so that we are left with . This can be done by .

Lastly, we move the powers of back to , leaving us with the result .

If we account for the different states we receive the following program.

I am not fully happy with it, since this performs only one Fibonaccy iteration. Actually, I wanted to have something that takes as input and gives me back or something similar. Anyway, I’ll leave this as an open question for the readers. We know that there is a solution due to the turing-completeness and I conjecture that it can be realized by augmenting my program from above with some more state bits. And yeah, I know that googling would maybe yield a solution, but this would take out all the fun.

## Conclusion

I hope I have given an understandable introduction into the magic of fractran. If you would like to play with it, you may want to try to implement multiplication (from repeated addition) or division with remainder (from repeated subtraction).