How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Mathematica / Wolfram Language programming language

Published on 22 June 2024 08:30 PM

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

This Wolfram code defines two functions, ffr and ffs, and then uses them to calculate and compare sequences of numbers.

ffr Function:

  • ffr[j_] calculates a sequence of numbers based on the input j.
  • It initializes three variables: R as a list containing the number 1, S as 2, and k as 1.
  • It then enters a Do loop that iterates j - 1 times.
  • Inside the loop, it uses a While loop to find the next number S that is not already in the list R.
  • Once S is found, it updates k and S, and then appends k to the list R.
  • Finally, it returns the list R.

ffs Function:

  • ffs[j_] calculates the differences between consecutive elements in the list returned by ffr[j + 1].
  • It uses the Differences function to compute the differences.

Usage of the Functions:

  • The code calculates ffr[10] and prints the result, which is the list {1, 3, 7, 12, 18, 26, 35, 45, 56, 69}.
  • It then calculates two more sequences: ffr[40] and ffs[960].
  • It joins these two sequences into a new list t and sorts it.
  • Finally, it compares t to the list Range[1000], which is a list of integers from 1 to 1000.

Output:

  • The output shows that t is equal to Range[1000], indicating that the two sequences are identical.

In summary, this code defines two functions, ffr and ffs, that generate sequences of numbers. It then uses these functions to calculate and compare specific sequences, showing that they are equal to the expected values.

Source code in the wolfram programming language

 ffr[j_] := Module[{R = {1}, S = 2, k = 1},
    Do[While[Position[R, S] != {}, S++]; k = k + S; S++;
    R = Append[R, k], {n, 1, j - 1}]; R]

 ffs[j_] := Differences[ffr[j + 1]]


 ffr[10]

 (* out *)
 {1, 3, 7, 12, 18, 26, 35, 45, 56, 69}


 t = Sort[Join[ffr[40], ffs[960]]];

 t == Range[1000]

 (* out *)
 True


  

You may also check:How to resolve the algorithm Trigonometric functions step by step in the AWK programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the J programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Raku programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Phix programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the BASIC programming language