These two sequences of positive integers are defined as:

 







R
(
1
)



=
1
 
;
 
S
(
1
)
=
2




R
(
n
)



=
R
(
n
−
1
)
+
S
(
n
−
1
)
,

n
>
1.






{\displaystyle {\begin{aligned}R(1)&=1\ ;\ S(1)=2\\R(n)&=R(n-1)+S(n-1),\quad n>1.\end{aligned}}}




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:

   1, 3, 7, 12, 18, ...

Sequence 



S


{\displaystyle S}

 starts:

   2, 4, 5, 6, 8, ...



Task

 Create two functions named ffr and ffs that when given n return R(n) or S(n) respectively.
(Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
 No maximum value for n should be assumed.
 Calculate and show that the first ten values of R are:
 1, 3, 7, 12, 18, 26, 35, 45, 56, and 69
 Calculate and show that the first 40 values of ffr plus the first 960 values of ffs include all the integers from 1 to 1000 exactly once.


References

 Sloane's A005228 and A030124.
 Wolfram MathWorld
 Wikipedia: Hofstadter Figure-Figure sequences.


      PRINT "First 10 values of R:"
      FOR i% = 1 TO 10 : PRINT ;FNffr(i%) " "; : NEXT : PRINT
      PRINT "First 10 values of S:"
      FOR i% = 1 TO 10 : PRINT ;FNffs(i%) " "; : NEXT : PRINT
      PRINT "Checking for first 1000 integers:"
      r% = 1 : s% = 1
      ffr% = FNffr(r%)
      ffs% = FNffs(s%)
      FOR wanted% = 1 TO 1000
        CASE TRUE OF
          WHEN wanted% = ffr% : r% += 1 : ffr% = FNffr(r%)
          WHEN wanted% = ffs% : s% += 1 : ffs% = FNffs(s%)
          OTHERWISE: EXIT FOR
        ENDCASE
      NEXT
      IF r% = 41 AND s% = 961 PRINT "Test passed" ELSE PRINT "Test failed"
      END
 
      DEF FNffr(N%)
      LOCAL I%, J%, R%, S%, V%
      DIM V% LOCAL 2*N%+1
      V%?1 = 1
      IF N% = 1 THEN = 1
      R% = 1
      S% = 2
      FOR I% = 2 TO N%
        FOR J% = S% TO 2*N%
          IF V%?J% = 0 EXIT FOR
        NEXT
        V%?J% = 1
        S% = J%
        R% += S%
        IF R% <= 2*N% V%?R% = 1
      NEXT I%
      = R%
 
      DEF FNffs(N%)
      LOCAL I%, J%, R%, S%, V%
      DIM V% LOCAL 2*N%+1
      V%?1 = 1
      IF N% = 1 THEN = 2
      R% = 1
      S% = 2
      FOR I% = 1 TO N%
        FOR J% = S% TO 2*N%
          IF V%?J% = 0 EXIT FOR
        NEXT
        V%?J% = 1
        S% = J%
        R% += S%
        IF R% <= 2*N% V%?R% = 1
      NEXT I%
      = S%