How to resolve the algorithm Hofstadter Q sequence step by step in the uBasic/4tH programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Q sequence step by step in the uBasic/4tH 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 uBasic/4tH programming language
Source code in the ubasic/4th programming language
Print "First 10 terms of Q = " ;
For i = 1 To 10 : Print FUNC(_q(i));" "; : Next : Print
Print "256th term = ";FUNC(_q(256))
End
_q Param(1)
Local(2)
If a@ < 3 Then Return (1)
If a@ = 3 Then Return (2)
@(0) = 1 : @(1) = 1 : @(2) = 2
c@ = 0
For b@ = 3 To a@-1
@(b@) = @(b@ - @(b@-1)) + @(b@ - @(b@-2))
If @(b@) < @(b@-1) Then c@ = c@ + 1
Next
Return (@(a@-1))
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Nim programming language
You may also check:How to resolve the algorithm Abstract type step by step in the ABAP programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Ruby programming language
You may also check:How to resolve the algorithm Tupper's self-referential formula step by step in the Lua programming language