How to resolve the algorithm Recaman's sequence step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Recaman's sequence step by step in the FreeBASIC 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 FreeBASIC programming language
Source code in the freebasic programming language
' version 26-01-2019
' compile with: fbc -s console
Dim As UByte used()
Dim As Integer sum, temp
Dim As UInteger n, max, count, i
max = 1000 : ReDim used(max)
Print "The first 15 terms are 0";
For n = 0 To 14
temp = sum - n
If temp < 1 OrElse used(temp) = 1 Then
temp = sum + n
End If
If temp <= max Then used(temp) = 1
sum = temp
Print sum;
Next
sum = 0 : max = 1000 : ReDim used(max)
Print : Print
For n = 0 To 50
temp = sum - n
If temp < 1 OrElse used(temp) = 1 Then
temp = sum + n
End If
If used(temp) = 1 Then
Print "First duplicated number is a(" + Str(n) + ")"
Exit For
End If
If temp <= max Then used(temp) = 1
sum = temp
Next
sum = 0 : max = 2000000 : ReDim used(max)
Print : Print
For n = 0 To max
temp = sum - n
If temp < 1 OrElse used(temp) = 1 Then
temp = sum + n
End If
If temp <= max Then used(temp) = 1
If i = temp Then
While used(i) = 1
i += 1
If i > 1000 Then
Exit For
End If
Wend
End If
sum = temp
count += 1
Next
Print "All integers from 0 to 1000 are generated in " & count & " terms"
Print
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End
You may also check:How to resolve the algorithm Dot product step by step in the Elena programming language
You may also check:How to resolve the algorithm Compound data type step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm Terminal control/Inverse video step by step in the 11l programming language
You may also check:How to resolve the algorithm Factorions step by step in the Sidef programming language
You may also check:How to resolve the algorithm String case step by step in the Plain English programming language