--- title: Hofstadter Figure-Figure sequences id: 59622f89e4e137560018a40e challengeType: 5 --- ## Description

These two sequences of positive integers are defined as:

$$R(1)=1\ ;\ S(1)=2 \\R(n)=R(n-1)+S(n-1), \quad n>1.$$

The sequence $S(n)$ is further defined as the sequence of positive integers not present in $R(n)$.

Sequence $R$ starts:

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

Sequence $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. Sloane's A005228 and A030124. Wolfram MathWorld Wikipedia: Hofstadter Figure-Figure sequences.
## Instructions
## Tests
```yml tests: - text: ffr is a function. testString: 'assert(typeof ffr === "function", "ffr is a function.");' - text: ffs is a function. testString: 'assert(typeof ffs === "function", "ffs is a function.");' - text: ffr should return integer. testString: 'assert(Number.isInteger(ffr(1)), "ffr should return integer.");' - text: ffs should return integer. testString: 'assert(Number.isInteger(ffs(1)), "ffs should return integer.");' - text: ffr() should return 69 testString: 'assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1], "ffr() should return 69");' - text: ffr() should return 1509 testString: 'assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1], "ffr() should return 1509");' - text: ffr() should return 5764 testString: 'assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1], "ffr() should return 5764");' - text: ffr() should return 526334 testString: 'assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1], "ffr() should return 526334");' - text: ffs() should return 14 testString: 'assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1], "ffs() should return 14");' - text: ffs() should return 59 testString: 'assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1], "ffs() should return 59");' - text: ffs() should return 112 testString: 'assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1], "ffs() should return 112");' - text: ffs() should return 1041 testString: 'assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1], "ffs() should return 1041");' ```
## Challenge Seed
```js // noprotect function ffr(n) { return n; } function ffs(n) { return n; } ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // noprotect const R = [null, 1]; const S = [null, 2]; function extendSequences (n) { let current = Math.max(R[R.length - 1], S[S.length - 1]); let i; while (R.length <= n || S.length <= n) { i = Math.min(R.length, S.length) - 1; current += 1; if (current === R[i] + S[i]) { R.push(current); } else { S.push(current); } } } function ffr (n) { extendSequences(n); return R[n]; } function ffs (n) { extendSequences(n); return S[n]; } ```