How to resolve the algorithm Recaman's sequence step by step in the uBasic/4tH programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Recaman's sequence step by step in the uBasic/4tH programming language

Table of Contents

Problem Statement

The Recamán's sequence generates Natural numbers. Starting from a(0)=0, the n'th term a(n), where n>0, is the previous term minus n i.e a(n) = a(n-1) - n but only if this is both positive and has not been previousely generated. If the conditions don't hold then a(n) = a(n-1) + n.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Recaman's sequence step by step in the uBasic/4tH programming language

Source code in the ubasic/4th programming language

a = 0                                  ' the first one is free ;-)
Print "First 15 numbers:"

For i = 1 Step 1                       ' start loop
  If i<16 Then Print a,                ' print first 15 numbers
  b = Iif ((a-i<1) + (Func(_Peek(Max(0, a-i)))), a+i, a-i)
  If Func(_Peek(b)) Then Print "\nFirst repetition: ";b : Break
  Proc _Set(Set(a, b))                 ' set bit in bitmap
Next
End                                    ' terminate program
                                       ' bitmap functions
_Set  Param(1) : Let @(a@/32) = Func(_Poke(a@/32, a@%32)) : Return
_Poke Param(2) : Return (Or(@(a@), Shl(1, b@)))
_Peek Param(1) : Return (And(@(a@/32), Shl(1, a@%32))>0)


  

You may also check:How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Ruby programming language
You may also check:How to resolve the algorithm Leap year step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Fusc sequence step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Dining philosophers step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Swift programming language