The Challenge
Write a program that acts as a Fractran interpreter. The shortest interpreter by character count, in any language, is the winner. Your program must take two inputs: The fractran program to be executed, and the input integer n. The program may be in any form that is convenient for your program - for example, a list of 2-tuples, or a flat list. The output must be a single integer, being the value of the register at the end of execution.
Fractran
Fractran is a trivial esoteric language invented by John Conway. A fractran program consists of a list of positive fractions and an initial state n. The interpreter maintains a program counter, initially pointing to the first fraction in the list. Fractran programs are executed in the following fashion:
- Check if the product of the current state and the fraction currently under the program counter is an integer. If it is, multiply the current state by the current fraction and reset the program counter to the beginning of the list.
- Advance the program counter. If the end of the list is reached, halt, otherwise return to step 1.
For details on how and why Fractran works, see the esolang entry and this entry on good math/bad math.
Test Vectors
Program: [(3, 2)]
Input: 72 (2332)
Output: 243 (35)
Program: [(3, 2)]
Input: 1296 (2434)
Output: 6561 (38)
Program: [(455, 33), (11, 13), (1, 11), (3, 7), (11, 2), (1, 3)]
Input: 72 (2332)
Output: 15625 (56)
Bonus test vector:
Your submission does not need to execute this last program correctly to be an acceptable answer. But kudos if it does!
Program: [(455, 33), (11, 13), (1, 11), (3, 7), (11, 2), (1, 3)]
Input: 60466176 (210310)
Output: 7888609052210118054117285652827862296732064351090230047702789306640625 (5100)
Submissions & Scoring
Programs are ranked strictly by length in characters - shortest is best. Feel free to submit both a nicely laid out and documented and a 'minified' version of your code, so people can see what's going on.
The language 'J' is not admissible. This is because there's already a well-known solution in J on one of the linked pages. If you're a J fan, sorry!
As an extra bonus, however, anyone who can provide a working fractran interpreter in fractran will receive a 500 reputation point bonus. In the unlikely event of multiple self-hosting interpreters, the one with the shortest number of fractions will receive the bounty.
Winners
The official winner, after submitting a self-hosting fractran solution comprising 1779 fractions, is Jesse Beder's solution. Practically speaking, the solution is too slow to execute even 1+1, however.
Incredibly, this has since been beaten by another fractran solution - Amadaeus's solution in only 84 fractions! It is capable of executing the first two test cases in a matter of seconds when running on my reference Python solution. It uses a novel encoding method for the fractions, which is also worth a close look.
Honorable mentions to:
- Stephen Canon's solution, in 165 characters of x86 assembly (28 bytes of machine code)
- Jordan's solution in 52 characters of ruby - which handles long integers
- Useless's solution in 87 characters of Python, which, although not the shortest Python solution, is one of the few solutions that isn't recursive, and hence handles harder programs with ease. It's also very readable.