HOME

TheInfoList



OR:

FRACTRAN is a
Turing-complete In computability theory, a system of data-manipulation rules (such as a computer's instruction set, a programming language, or a cellular automaton) is said to be Turing-complete or computationally universal if it can be used to simulate any Tur ...
esoteric programming language An esoteric programming language (sometimes shortened to esolang) is a programming language designed to test the boundaries of computer programming language design, as a proof of concept, as software art, as a hacking interface to another language ...
invented by the mathematician John Conway. A FRACTRAN program is an ordered list of positive
fractions A fraction (from la, fractus, "broken") represents a part of a whole or, more generally, any number of equal parts. When spoken in everyday English, a fraction describes how many parts of a certain size there are, for example, one-half, eight ...
together with an initial positive integer input ''n''. The program is run by updating the integer ''n'' as follows: #for the first fraction ''f'' in the list for which ''nf'' is an integer, replace ''n'' by ''nf'' #repeat this rule until no fraction in the list produces an integer when multiplied by ''n'', then halt. gives the following FRACTRAN program, called PRIMEGAME, which finds successive
prime number A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
s: \left( \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac \right) Starting with ''n''=2, this FRACTRAN program generates the following sequence of integers: * 2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, ... After 2, this sequence contains the following powers of 2: 2^2=4,\, 2^3=8,\, 2^5=32,\, 2^7=128,\, 2^=2048,\, 2^=8192,\, 2^=131072,\, 2^=524288,\, \dots which are the prime powers of 2.


Understanding a FRACTRAN program

A FRACTRAN program can be seen as a type of
register machine In mathematical logic and theoretical computer science a register machine is a generic class of abstract machines used in a manner similar to a Turing machine. All the models are Turing equivalent. Overview The register machine gets its name from ...
where the registers are stored in prime exponents in the argument ''n''. Using
Gödel numbering In mathematical logic, a Gödel numbering is a function that assigns to each symbol and well-formed formula of some formal language a unique natural number, called its Gödel number. The concept was developed by Kurt Gödel for the proof of his ...
, a positive integer ''n'' can encode an arbitrary number of arbitrarily large positive integer variables.
Gödel numbering In mathematical logic, a Gödel numbering is a function that assigns to each symbol and well-formed formula of some formal language a unique natural number, called its Gödel number. The concept was developed by Kurt Gödel for the proof of his ...
cannot be directly used for negative integers, floating point numbers or text strings, although conventions could be adopted to represent these data types indirectly. Proposed extensions to FRACTRAN includ
FRACTRAN++
an
Bag
The value of each variable is encoded as the exponent of a prime number in the
prime factorization In number theory, integer factorization is the decomposition of a composite number into a product of smaller integers. If these factors are further restricted to prime numbers, the process is called prime factorization. When the numbers are su ...
of the integer. For example, the integer 60 = 2^2 \times 3^1 \times 5^1 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 A fraction (from la, fractus, "broken") represents a part of a whole or, more generally, any number of equal parts. When spoken in everyday English, a fraction describes how many parts of a certain size there are, for example, one-half, eight ...
. For example: f_1 = \frac = \frac tests v2 and v5. If v_2 \ge 2 and v_5 \ge 1, then it subtracts 2 from v2 and 1 from v5 and adds 1 to v3 and 1 to v7. For example: 60 \cdot f_1 = 2^2 \times 3^1 \times 5^1 \cdot \frac = 3^2 \times 7^1 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: *Each time an instruction is executed, the variables that are tested are also decremented. *The same variable cannot be both decremented and incremented in a single instruction (otherwise the fraction representing that instruction would not be in its
lowest terms An irreducible fraction (or fraction in lowest terms, simplest form or reduced fraction) is a fraction in which the numerator and denominator are integers that have no other common divisors than 1 (and −1, when negative numbers are considered). I ...
). Therefore each FRACTRAN instruction consumes variables as it tests them. *It is not possible for a FRACTRAN instruction to directly test if a variable is 0 (However, an indirect test can be implemented by creating a default instruction that is placed after other instructions that test a particular variable.).


Creating simple programs


Addition

The simplest FRACTRAN program is a single instruction such as \left( \frac \right) This program can be represented as a (very simple) algorithm as follows: Given an initial input of the form 2^a 3^b, this program will compute the sequence 2^ 3^, 2^ 3^, etc., until eventually, after a steps, no factors of 2 remain and the product with \frac no longer yields an integer; the machine then stops with a final output of 3^ . It therefore adds two integers together.


Multiplication

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 2^a 3^b and produce 5^: 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: 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: \left( \frac, \frac, \frac, \frac, \frac, \frac \right) With input 2''a''3''b'' this program produces output 5''ab''. A similar multiplier algorithm is described at th
Esolang FRACTRAN page


Subtraction and division

In a similar way, we can create a FRACTRAN "subtracter", and repeated subtractions allow us to create a "quotient and remainder" algorithm as follows: Writing out the FRACTRAN program, we have: \left( \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac \right) and input 2''n''3''d''11 produces output 5''q''7''r'' where ''n'' = ''qd'' + ''r'' and 0 ≤ ''r'' < ''d''.


Conway's prime algorithm

Conway's prime generating algorithm above is essentially a quotient and remainder algorithm within two loops. Given input of the form 2^n 7^m where 0 ≤ ''m'' < ''n'', the algorithm tries to divide ''n''+1 by each number from ''n'' down to 1, until it finds the largest number ''k'' that is a divisor of ''n''+1. It then returns 2''n''+1 7''k''-1 and repeats. The only times that the sequence of state numbers generated by the algorithm produces a power of 2 is when ''k'' is 1 (so that the exponent of 7 is 0), which only occurs if the exponent of 2 is a prime. A step-by-step explanation of Conway's algorithm can be found in Havil (2007). 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: \left( \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac \right) 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: N - 1 + (6N+2)(N-b) + 2 \sum\limits^_ \left\lfloor \frac \right\rfloor, where b < N is the largest integer divisor of ''N'' and \lfloor x \rfloor is the
floor function In mathematics and computer science, the floor function is the function that takes as input a real number , and gives as output the greatest integer less than or equal to , denoted or . Similarly, the ceiling function maps to the least i ...
. In 1999, Devin Kilminster demonstrated a shorter, ten-instruction program: \left( \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac, \frac \right). For the initial input ''n = 10'' successive primes are generated by subsequent powers of 10.


Other examples

The following FRACTRAN program: \left( \frac , \frac, \frac, \frac, \frac, \frac, \frac \right) calculates the
Hamming weight The Hamming weight of a string is the number of symbols that are different from the zero-symbol of the alphabet used. It is thus equivalent to the Hamming distance from the all-zero string of the same length. For the most typical case, a string of ...
H(''a'') of the binary expansion of ''a'' i.e. the number of 1s in the binary expansion of ''a''.John Baez
Puzzle #4
The ''n''-Category Café
Given input 2''a'', its output is 13H(''a''). The program can be analysed as follows:


Notes


See also

*
One-instruction set computer A one-instruction set computer (OISC), sometimes called an ultimate reduced instruction set computer (URISC), is an abstract machine that uses only one instructionobviating the need for a machine language opcode. With a judicious choice for the si ...


References

* * * * *


External links


Lecture from John Conway: "Fractran: A Ridiculous Logical Language""Prime Number Pathology: Fractran"
* {{MathWorld , urlname=FRACTRAN , title=FRACTRAN
''Prime Number Pathology''FRACTRAN
- (Esolang wiki)
Project Euler Problem 308"Building Fizzbuzz in Fractran from the Bottom Up"Chris Lomont, "A Universal FRACTRAN Interpreter in FRACTRAN"
Models of computation Esoteric programming languages Recreational mathematics