How to resolve the algorithm Hofstadter Q sequence step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Q sequence step by step in the Wren 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 Wren programming language
Source code in the wren programming language
var N = 1e5
var q = List.filled(N + 1, 0)
q[1] = 1
q[2] = 1
for (n in 3..N) q[n] = q[n - q[n-1]] + q[n - q[n-2]]
System.print("The first ten terms of the Hofstadter Q sequence are:")
System.print(q[1..10])
System.print("\nThe thousandth term is %(q[1000]).")
var flips = 0
for (n in 2..N) {
if (q[n] < q[n-1]) flips = flips + 1
}
System.print("\nThere are %(flips) flips in the first %(N) terms.")
You may also check:How to resolve the algorithm Calendar step by step in the Overview programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the C++ programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Yorick programming language