Hofstadter Q sequence
From Fōrmulæ wiki
This page is the answer to the task Hofstadter Q sequence in the Rosetta Code.
Contents
Description (from Rosetta Code)
The Hofstadter Q sequence is defined as:
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. Task
Optional extra credit
|
Solution
The following script defines a function to create a list with the first n terms of the sequence.
Case 1. First ten terms
Case 2. 1000th term
The x list is created containing the first 1000,000 terms. It will be used for this case and the next one:
Obtaining the 1000th term:
Case 3. Extra credit
The following script counts how many times a member of the x list is less than its preceding term: