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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the CoffeeScript 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 CoffeeScript programming language

Source code in the coffeescript programming language

R = [ null, 1 ]
S = [ null, 2 ]

extend_sequences = (n) ->
  current = Math.max(R[R.length - 1], S[S.length - 1])
  i = undefined
  while R.length <= n or S.length <= n
    i = Math.min(R.length, S.length) - 1
    current += 1
    if current == R[i] + S[i]
      R.push current
    else
      S.push current

ff = (X, n) ->
    extend_sequences n
    X[n]

console.log 'R(' + i + ') = ' + ff(R, i) for i in [1..10]
int_array = ([1..40].map (i) -> ff(R, i)).concat [1..960].map (i) -> ff(S, i)
int_array.sort (a, b) -> a - b

for i in [1..1000]
  if int_array[i - 1] != i
    throw 'Something\'s wrong!'
console.log '1000 integer check ok.'


  

You may also check:How to resolve the algorithm Palindrome detection step by step in the Frink programming language
You may also check:How to resolve the algorithm Window creation step by step in the Java programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Fortran programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the ERRE programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Vedit macro language programming language