How to resolve the algorithm Hofstadter Q sequence step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hofstadter Q sequence step by step in the Factor 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 Factor programming language

Source code in the factor programming language

( scratchpad ) : next ( seq -- newseq )
dup 2 tail* over length [ swap - ] curry map
[ dupd swap nth ] map 0 [ + ] reduce suffix ;

( scratchpad ) { 1 1 } 1000 [ next ] times  dup 10 head .  999 swap nth .
{ 1 1 2 3 3 4 5 5 6 6 }
502


  

You may also check:How to resolve the algorithm URL encoding step by step in the R programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Golden ratio/Convergence step by step in the RATFOR programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the RPL programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the Racket programming language