How to resolve the algorithm Hofstadter Q sequence step by step in the AutoHotkey programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Q sequence step by step in the AutoHotkey 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 AutoHotkey programming language
Source code in the autohotkey programming language
SetBatchLines, -1
Q := HofsQSeq(100000)
Loop, 10
Out .= Q[A_Index] ", "
MsgBox, % "First ten:`t" Out "`n"
. "1000th:`t`t" Q[1000] "`n"
. "Flips:`t`t" Q.flips
HofsQSeq(n) {
Q := {1: 1, 2: 1, "flips": 0}
Loop, % n - 2 {
i := A_Index + 2
, Q[i] := Q[i - Q[i - 1]] + Q[i - Q[A_Index]]
if (Q[i] < Q[i - 1])
Q.flips++
}
return Q
}
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the C programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the Raku programming language
You may also check:How to resolve the algorithm Water collected between towers step by step in the Phix programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the REXX programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the XBS programming language