How to resolve the algorithm Fractran step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fractran step by step in the OCaml 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 OCaml programming language

Source code in the ocaml programming language

open Num

let get_input () =
   num_of_int (
     try int_of_string Sys.argv.(1)
     with _ -> 10)

let get_max_steps () =
   try int_of_string Sys.argv.(2)
   with _ -> 50
   
let read_program () =
   let line = read_line () in
   let words = Str.split (Str.regexp " +") line in
   List.map num_of_string words

let is_int n = n =/ (integer_num n)

let run_program num prog =

   let replace n =
      let rec step = function
      | [] -> None
      | h :: t ->
            let n' = h */ n in
            if is_int n' then Some n' else step t in
      step prog in

   let rec repeat m lim =
      Printf.printf "  %s\n" (string_of_num m);
      if lim = 0 then print_endline "Reached max step limit" else
         match replace m with
         | None -> print_endline "Finished"
         | Some x -> repeat x (lim-1)
   in

   let max_steps = get_max_steps () in
   repeat num max_steps

let () =
   let num = get_input () in
   let prog = read_program () in
   run_program num prog


  

You may also check:How to resolve the algorithm Jump anywhere step by step in the SPL programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Machine code step by step in the Pascal programming language
You may also check:How to resolve the algorithm Compound data type step by step in the TXR programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Ruby programming language