How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

These two sequences of positive integers are defined as:

The sequence

S ( n )

{\displaystyle S(n)}

is further defined as the sequence of positive integers not present in

R ( n )

{\displaystyle R(n)}

. Sequence

R

{\displaystyle R}

starts: Sequence

S

{\displaystyle S}

starts:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the FreeBASIC programming language

Source code in the freebasic programming language

function ffr( n as integer ) as integer
    if n = 1 then return 1
    dim as integer i, j, r=1, s=1, v(1 to 2*n+1)
    v(1) = 1
    for i = 2 to n
        for j = s to 2*n
            if v(j) = 0 then exit for
        next j
        v(j) = 1
        s = j
        r += s
        if r <= 2*n then v(r) = 1
    next i
    return r
end function

function ffs( n as integer ) as integer
    if n = 1 then return 2
    dim as integer i, j, r=1, s=2, v(1 to 2*n+1)
    for i = 1 to n
        for j = s to 2*n
            if v(j) = 0 then exit for
        next j
        v(j) = 1
        s = j
        r += s
        if r <= 2*n then v(r) = 1
    next i
    return s
end function

dim as integer i
print " R"," S"
print
for i = 1 to 10
    print ffr(i), ffs(i)
next i

dim as boolean found(1 to 1000), failed
for i = 1 to 40
    found(ffr(i)) = true
next i
for i = 1 to 960
    found(ffs(i)) = true
next i
for i = 1 to 1000
    if found(i) = false then failed = true
next i

if failed then print "Oh no!" else print "All integers from 1 to 1000 accounted for"

  

You may also check:How to resolve the algorithm Equal prime and composite sums step by step in the Lua programming language
You may also check:How to resolve the algorithm Empty program step by step in the Lang programming language
You may also check:How to resolve the algorithm Search a list step by step in the Go programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the PHP programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Lasso programming language