How to resolve the algorithm Hofstadter Q sequence step by step in the PicoLisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Q sequence step by step in the PicoLisp 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 PicoLisp programming language
Source code in the picolisp programming language
(de q (N)
(cache '(NIL) N
(if (>= 2 N)
1
(+
(q (- N (q (dec N))))
(q (- N (q (- N 2)))) ) ) ) )
: (mapcar q (range 1 10))
-> (1 1 2 3 3 4 5 5 6 6)
: (q 1000)
-> 502
: (let L (mapcar q (range 1 100000))
(cnt < (cdr L) L) )
-> 49798
You may also check:How to resolve the algorithm Sum of a series step by step in the Raku programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Scala programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the OCaml programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm SOAP step by step in the Smalltalk programming language