# Golf a FRACTRAN interpreter

+4

−0

# Description

From the Esolangs wiki,

In Fractran,

- a program consists of a finite list of positive, rational numbers.
- The input to a program is a positive integer n.
- The list is then searched in order, for a rational number $p/q$ such that $n×p/q$ is an integer.
- Then n is replaced by $n×p/q$ and the search is restarted from the beginning of the list.
- The program halts when no such number $p/q$ can be found, and the final n becomes the output from the program.
- Output the final value of $n$.

Your task is to implement an interpreter for this language.

# Input

You are to take two inputs:

- $n$, an integer
- $A$, an array of fractions, which may be taken as a list of pairs, or in the rational datatype of your language.

# Output

A single integer, the final value of $n$.

# Test Cases

Formatted as

```
program
input
output
```

```
78/55, 5/3, 1/5, 11/2, 5/7
1096135733
328842888196762472689573703
3/2
1296
6561
455/33, 11/13, 1/11, 3/7, 11/2, 1/3
72
15625
```

## 2 answers

+4

−0

# APL (Dyalog Unicode), 33 bytes

```
{×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺}
```

The first case doesn't work because it gets really big, but the other two do. The input is taken on the left and the fractions are taken as a table on the right.

```
{×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺}
⍺× ⍝ n multiplied by
1⌷⍵ ⍝ The first row of the right argument (every p)
(2⌷⍵) ⍝ Second row of right arg (all q's)
| ⍝ All n×p modulo q
0= ⍝ Check which ones are 0 (rational)
÷⍨ ⍝ Make another vector of 'n×p÷q's
/⍨ ⍝ And keep the ones that were rational
⊃ ⍝ Pick the first (0 if empty)
x← ⍝ Assign to x
× ⍝ Sign of x
⍺ ⍝ If sign is 0, return n
x∇⍵ ⍝ Otherwise, call again with x as new n
```

#### 2 comments

finding tests for this is a bit of a pain.. everything's in some weird modulo or prime factorization format aaaaa

I'll change the first test case to your output in `⎕FR←1287⋄⎕PP←34`

+1

−0

# Python 3, 76 bytes

```
def f(p,n):l=[n*p//q for(p,q)in p if n%q==0];return n if l==[]else f(p,l[0])
```

This code assumes that the fractions are given as completely cancelled pairs of integers.

## 2 comments

I don't understand how the second test case can ever terminate. Since there is a fraction 55/1 at the end and two integers produce an integer when multiplied, it would never get past that fraction. — user 13 days ago

yeah, I wrote that wrong. It's an infinite loop which cycles through 2^n, where n is prime. — Razetime 13 days ago