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.


var cr = @[1]
var cs = @[2]
 
proc extendRS =
  let x = cr[cr.high] + cs[cr.high]
  cr.add x
  for y in cs[cs.high] + 1 ..< x: cs.add y
  cs.add x + 1
 
proc ffr(n: int): int =
  assert n > 0
  while n > cr.len: extendRS()
  cr[n - 1]
 
proc ffs(n: int): int =
  assert n > 0
  while n > cs.len: extendRS()
  cs[n - 1]
 
for i in 1..10: stdout.write ffr i," "
echo ""
 
var bin: array[1..1000, int]
for i in 1..40: inc bin[ffr i]
for i in 1..960: inc bin[ffs i]
var all = true
for x in bin:
  if x != 1:
    all = false
    break
 
if all: echo "All Integers 1..1000 found OK"
else: echo "All Integers 1..1000 NOT found only once: ERROR"