2018-10-10 22:03:03 +00:00
---
id: 59622f89e4e137560018a40e
2021-02-06 04:42:36 +00:00
title: Hofstadter Figure-Figure sequences
2018-10-10 22:03:03 +00:00
challengeType: 5
2021-02-06 04:42:36 +00:00
forumTopicId: 302286
2021-01-13 02:31:00 +00:00
dashedName: hofstadter-figure-figure-sequences
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
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:
< pre > 1, 3, 7, 12, 18, ...< / pre >
Sequence $S$ starts:
< pre > 2, 4, 5, 6, 8, ...< / pre >
# --instructions--
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.
**References**
< ul >
< li >
Sloane's < a href = 'https://oeis.org/A005228' target = '_blank' > A005228< / a > and < a href = 'https://oeis.org/A030124' target = '_blank' > A030124< / a > .
< / li >
< li >
Wikipedia: < a href = 'https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences' title = 'wp: Hofstadter_sequence#Hofstadter_Figure-Figure_sequences' target = '_blank' > Hofstadter Figure-Figure sequences< / a > .
< / li >
< / ul >
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`ffr` should be a function.
2020-12-16 07:37:30 +00:00
```js
assert(typeof ffr === 'function');
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`ffs` should be a function.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(typeof ffs === 'function');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`ffr` should return integer.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(Number.isInteger(ffr(1)));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`ffs` should return integer.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(Number.isInteger(ffs(1)));
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`ffr(10)` should return `69`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1]);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`ffr(50)` should return `1509`
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1]);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`ffr(100)` should return `5764`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1]);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`ffr(1000)` should return `526334`
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1]);
2018-10-10 22:03:03 +00:00
```
2020-08-13 15:24:35 +00:00
2021-02-06 04:42:36 +00:00
`ffs(10)` should return `14`
2020-12-16 07:37:30 +00:00
```js
assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1]);
```
2021-02-06 04:42:36 +00:00
`ffs(50)` should return `59`
2020-12-16 07:37:30 +00:00
```js
assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1]);
```
2021-02-06 04:42:36 +00:00
`ffs(100)` should return `112`
2020-12-16 07:37:30 +00:00
```js
assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1]);
```
2021-02-06 04:42:36 +00:00
`ffs(1000)` should return `1041`
2020-12-16 07:37:30 +00:00
```js
assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1]);
```
2021-01-13 02:31:00 +00:00
# --seed--
## --after-user-code--
```js
const ffrParamRes = [[10, 69], [50, 1509], [100, 5764], [1000, 526334]];
const ffsParamRes = [[10, 14], [50, 59], [100, 112], [1000, 1041]];
```
## --seed-contents--
```js
function ffr(n) {
return n;
}
function ffs(n) {
return n;
}
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
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];
}
```