How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the CLU programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the CLU 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 CLU programming language
Source code in the clu programming language
figfig = cluster is ffr, ffs
rep = null
ai = array[int]
own R: ai := ai$[1]
own S: ai := ai$[2]
% Extend R and S until R(n) is known
extend = proc (n: int)
while n > ai$high(R) do
next: int := ai$top(R) + S[ai$high(R)]
ai$addh(R, next)
while ai$top(S) < next-1 do
ai$addh(S, ai$top(S)+1)
end
ai$addh(S, next+1)
end
end extend
ffr = proc (n: int) returns (int)
extend(n)
return(R[n])
end ffr
ffs = proc (n: int) returns (int)
while n > ai$high(S) do
extend(ai$high(R) + 1)
end
return(S[n])
end ffs
end figfig
start_up = proc ()
ai = array[int]
po: stream := stream$primary_output()
% Print R[1..10]
stream$puts(po, "R[1..10] =")
for i: int in int$from_to(1,10) do
stream$puts(po, " " || int$unparse(figfig$ffr(i)))
end
stream$putl(po, "")
% Count the occurrences of 1..1000 in R[1..40] and S[1..960]
occur: ai := ai$fill(1, 1000, 0)
for i: int in int$from_to(1, 40) do
occur[figfig$ffr(i)] := occur[figfig$ffr(i)] + 1
end
for i: int in int$from_to(1, 960) do
occur[figfig$ffs(i)] := occur[figfig$ffs(i)] + 1
end
% See if they all occur exactly once
begin
for i: int in int$from_to(1, 1000) do
if occur[i] ~= 1 then exit wrong(i) end
end
stream$putl(po,
"All numbers 1..1000 occur exactly once in R[1..40] U S[1..960].")
end except when wrong(i: int):
stream$putl(po, "Error: " ||
int$unparse(i) || " occurs " || int$unparse(occur[i]) || " times.")
end
end start_up
You may also check:How to resolve the algorithm Roots of a function step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Terminal control/Positional read step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Prolog programming language