How to resolve the algorithm Hofstadter Q sequence step by step in the Visual FoxPro programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Q sequence step by step in the Visual FoxPro 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 Visual FoxPro programming language
Source code in the visual programming language
LOCAL p As Integer, i As Integer
CLEAR
p = 0
? "Hofstadter Q Sequence"
? "First 10 terms:"
FOR i = 1 TO 10
?? Q(i, @p)
ENDFOR
? "1000th term:", Q(1000, @p)
? "100000th term:", q(100000, @p)
? "Number of terms less than the preceding term:", p
FUNCTION Q(n As Integer, k As Integer) As Integer
LOCAL i As Integer
LOCAL ARRAY aq[n]
aq[1] = 1
IF n > 1
aq[2] = 1
ENDIF
k = 0
FOR i = 3 TO n
aq[i] = aq[i - aq[i-1]] + aq[i-aq[i-2]]
IF aq(i) < aq(i-1)
k = k + 1
ENDIF
ENDFOR
RETURN aq[n]
ENDFUNC
You may also check:How to resolve the algorithm Reverse a string step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Ring programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Lasso programming language
You may also check:How to resolve the algorithm First class environments step by step in the Clojure programming language