user
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 {\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 {\displaystyle S(n)}}
 is further defined as the sequence of positive integers ***not*** present in 

R
(
n
)
{\displaystyle {\displaystyle R(n)}}
.

Sequence 

R
{\displaystyle {\displaystyle R}}
 starts:

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

```

Sequence 

S
{\displaystyle {\displaystyle S}}
 starts:

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

```

Task

1. 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).
2. No maximum value for **n** should be assumed.
3. Calculate and show that the first ten values of **R** are:  
    1, 3, 7, 12, 18, 26, 35, 45, 56, and 69
4. 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.
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2018.03

```
my %r = 1 => 1;
my %s = 1 => 2;

sub ffr ($n) { %r{$n} //= ffr($n - 1) + ffs($n - 1) }
sub ffs ($n) { %s{$n} //= (grep none(map &ffr, 1..$n), max(%s.values)+1..*)[0] }

my @ffr = map &ffr, 1..*;
my @ffs = map &ffs, 1..*;

say @ffr[^10];
say "Rawks!" if 1...1000 eqv sort |@ffr[^40], |@ffs[^960];

```

Output:

```
1 3 7 12 18 26 35 45 56 69
Rawks!
```