Fractran interpreter def fractran(n, prog, giveup=1000): John Conway, who was quite a character among mathematicians, is best known for The Game of Life, not to be confused with the family board game sharing the same name. That achievement eclipsed basically all other wacky and original creations of Conway, so in the name of fairness, we ought to give his less appreciated creations an occasional turn in the flashing lights of the red carpet fame. This lab has you write an interpreter for the simple esoteric programming language called FRACTRAN, named as a pun of the word “fraction” and the FORTRAN programming language. (Things used to be all in uppercase back in when real scientists wore horn-rimmed glasses and their cars were equipped with tail fins, with occasional dollar signs interspersed as separators to keep the pampered prices of the military-industrial complex happy.) A program written in such mysterious and hard-to-decipher form consists of nothing but a list of positive integer fractions, in this problem given as tuples of the numerator and the denominator. Of course, you are not merely allowed but encouraged to use the Fraction data type of the Python fractions module to simplify the computations inside your function. Given a positive integer n as its start state, the next state is the product n*f for the first fraction listed in prog for which n*f is an exact integer. That integer then becomes the new state for the next round. Once n*f is not an integer for any of the fractions f listed in prog, the execution terminates. Your function should compute and return the sequence of integers produced by the given FRACTRAN program, with a forced termina... sorry, forced halting (after all, Conway was British) taking place after giveup steps, if the execution has not halted by then.
Fractran interpreter
def fractran(n, prog, giveup=1000):
John Conway, who was quite a character among mathematicians, is best known for The Game of Life, not to be confused with the family board game sharing the same name. That achievement eclipsed basically all other wacky and original creations of Conway, so in the name of fairness, we ought to
give his less appreciated creations an occasional turn in the flashing lights of the red carpet fame.
This lab has you write an interpreter for the simple esoteric
Given a positive integer n as its start state, the next state is the product n*f for the first fraction listed in prog for which n*f is an exact integer. That integer then becomes the new state for the next round. Once n*f is not an integer for any of the fractions f listed in prog, the execution
terminates. Your function should compute and return the sequence of integers produced by the given FRACTRAN program, with a forced termina... sorry, forced halting (after all, Conway was British) taking place after giveup steps, if the execution has not halted by then.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps