RosettaCodeData/Task/Hofstadter-Figure-Figure-sequences/FreeBASIC/hofstadter-figure-figure-sequences.basic
2023-07-01 13:44:08 -04:00

50 lines
1 KiB
Text

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"