How to resolve the algorithm Fractran step by step in the Scala programming language
How to resolve the algorithm Fractran step by step in the Scala programming language
Table of Contents
Problem Statement
FRACTRAN is a Turing-complete esoteric programming language invented by the mathematician John Horton Conway. A FRACTRAN program is an ordered list of positive fractions
P
(
f
1
,
f
2
, … ,
f
m
)
{\displaystyle P=(f_{1},f_{2},\ldots ,f_{m})}
, together with an initial positive integer input
n
{\displaystyle n}
.
The program is run by updating the integer
n
{\displaystyle n}
as follows:
Conway gave a program for primes in FRACTRAN: Starting with
n
2
{\displaystyle n=2}
, this FRACTRAN program will change
n
{\displaystyle n}
to
15
2 × ( 15
/
2 )
{\displaystyle 15=2\times (15/2)}
, then
825
15 × ( 55
/
1 )
{\displaystyle 825=15\times (55/1)}
, generating the following sequence of integers: After 2, this sequence contains the following powers of 2: which are the prime powers of 2.
Write a program that reads a list of fractions in a natural format from the keyboard or from a string,
to parse it into a sequence of fractions (i.e. two integers),
and runs the FRACTRAN starting from a provided integer, writing the result at each step.
It is also required that the number of steps is limited (by a parameter easy to find).
Use this program to derive the first 20 or so prime numbers.
For more on how to program FRACTRAN as a universal programming language, see:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fractran step by step in the Scala programming language
Source code in the scala programming language
class TestFractran extends FunSuite {
val program = Fractran("17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1")
val expect = List(2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 132)
test("find first fifteen fractran figures") {
assert((program .execute(2) take 15 toList) === expect)
}
}
object Fractran {
val pattern = """\s*(\d+)\s*/\s*(\d+)\s*""".r
def parse(m: Match) = ((m group 1).toInt, (m group 2).toInt)
def apply(program: String) = new Fractran(
pattern.findAllMatchIn(program).map(parse).toList)
}
class Fractran(val numDem: List[(Int,Int)]) {
def execute(value: Int) = unfold(value) { v =>
numDem indexWhere(v % _._2 == 0) match {
case i if i > -1 => Some(v, numDem(i)._1 * v / numDem(i)._2)
case _ => None
}
}
}
You may also check:How to resolve the algorithm Active Directory/Connect step by step in the Go programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the jq programming language
You may also check:How to resolve the algorithm Classes step by step in the Slate programming language
You may also check:How to resolve the algorithm Algebraic data types step by step in the Nim programming language
You may also check:How to resolve the algorithm Prime conspiracy step by step in the ALGOL 68 programming language