How to resolve the algorithm Hofstadter Q sequence step by step in the SETL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Q sequence step by step in the SETL programming language
Table of Contents
Problem Statement
It is defined like the Fibonacci sequence, but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.
(This point is to ensure that caching and/or recursion limits, if it is a concern, is correctly handled).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Hofstadter Q sequence step by step in the SETL programming language
Source code in the setl programming language
program hofstadter_q;
q := [1,1];
loop for n in [3..100000] do
q(n) := q(n-q(n-1)) + q(n-q(n-2));
end loop;
print("First 10 terms: " + q(1..10));
print("1000th term: " + q(1000));
print("q(x) < q(x-1): " + #[x : x in [2..#q] | q(x) < q(x-1)]);
end program;
You may also check:How to resolve the algorithm Hostname step by step in the REXX programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the DCL programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Oz programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the Component Pascal programming language