2018-09-30 23:01:58 +01:00
---
title: Hofstadter Figure-Figure sequences
id: 59622f89e4e137560018a40e
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302286
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-03-05 18:37:06 +09:00
These two sequences of positive integers are defined as:
< span style = "margin-left: 2em;" >< big > $R(1)=1\ ;\ S(1)=2 \\R(n)=R(n-1)+S(n-1), \quad n>1.$</ big ></ span >
The sequence < big > $S(n)$< / big > is further defined as the sequence of positive integers not present in < big > $R(n)$< / big > .
Sequence < big > $R$< / big > starts:
< pre > 1, 3, 7, 12, 18, ...< / pre >
Sequence < big > $S$< / big > starts:
< pre > 2, 4, 5, 6, 8, ...< / pre >
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-06-14 20:04:16 +09:00
Create two functions named < code > ffr< / code > and < code > ffs< / code > that when given < code > n< / code > return < code > R(n)< / code > or < code > S(n)< / code > respectively. (Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
No maximum value for < code > n< / code > should be assumed.
< strong > References< / strong >
2019-03-05 18:37:06 +09:00
< ul >
< li >
2019-05-22 23:30:29 +09:00
Sloane's < a href = "https://oeis.org/A005228" target = "_blank" > A005228< / a > and < a href = "https://oeis.org/A030124" target = "_blank" > A030124< / a > .
2019-03-05 18:37:06 +09:00
< / 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 >
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-20 07:01:31 -08:00
- text: < code > ffr</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof ffr === 'function');
2019-11-20 07:01:31 -08:00
- text: < code > ffs</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof ffs === 'function');
2018-10-04 14:37:37 +01:00
- text: < code > ffr</ code > should return integer.
2019-07-26 05:24:52 -07:00
testString: assert(Number.isInteger(ffr(1)));
2018-10-04 14:37:37 +01:00
- text: < code > ffs</ code > should return integer.
2019-07-26 05:24:52 -07:00
testString: assert(Number.isInteger(ffs(1)));
2018-10-04 14:37:37 +01:00
- text: < code > ffr()</ code > should return < code > 69</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffr()</ code > should return < code > 1509</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffr()</ code > should return < code > 5764</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffr()</ code > should return < code > 526334</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffs()</ code > should return < code > 14</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffs()</ code > should return < code > 59</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffs()</ code > should return < code > 112</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1]);
2018-10-04 14:37:37 +01:00
- text: < code > ffs()</ code > should return < code > 1041</ code >
2019-07-26 05:24:52 -07:00
testString: assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1]);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// noprotect
function ffr(n) {
return n;
}
function ffs(n) {
return n;
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const ffrParamRes = [[10, 69], [50, 1509], [100, 5764], [1000, 526334]];
const ffsParamRes = [[10, 14], [50, 59], [100, 112], [1000, 1041]];
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = '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];
}
```
< / section >