How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Wren programming language

Table of Contents

Problem Statement

These two sequences of positive integers are defined as:

The sequence

S ( n )

{\displaystyle S(n)}

is further defined as the sequence of positive integers not present in

R ( n )

{\displaystyle R(n)}

. Sequence

R

{\displaystyle R}

starts: Sequence

S

{\displaystyle S}

starts:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Wren programming language

Source code in the wren programming language

var r = [0, 1]
var s = [0, 2]

var ffr = Fn.new { |n|
    while (r.count <= n) {
        var nrk = r.count - 1         // last n for which r[n] is known
        var rNxt = r[nrk] + s[nrk]    // r[nrk+1]
        r.add(rNxt)                   // extend r by one element
        for (sn in r[nrk]+2...rNxt) {
            s.add(sn)                 // extend sequence s up to rNxt
        }
        s.add(rNxt + 1)               // extend sequence s one past rNxt
    }
    return r[n]
}

var ffs = Fn.new { |n|
    while (s.count <= n) ffr.call(r.count)
    return s[n]
}

System.print("The first 10 values of R are:")
for (i in 1..10) System.write(" %(ffr.call(i))")
System.print()
var present = List.filled(1001, false)
for (i in 1..40)  present[ffr.call(i)] = true
for (i in 1..960) present[ffs.call(i)] = true
var allPresent = present.skip(1).all { |i| i == true }
System.print("\nThe first 40 values of ffr plus the first 960 values of ffs")
System.print("includes all integers from 1 to 1000 exactly once is %(allPresent).")


  

You may also check:How to resolve the algorithm Resistor mesh step by step in the ERRE programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Order programming language
You may also check:How to resolve the algorithm Sylvester's sequence step by step in the PL/M programming language
You may also check:How to resolve the algorithm Gamma function step by step in the Octave programming language
You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Rust programming language