3.7 KiB
3.7 KiB
title | id | challengeType | forumTopicId |
---|---|---|---|
Hofstadter Figure-Figure sequences | 59622f89e4e137560018a40e | 5 | 302286 |
Description
1, 3, 7, 12, 18, ...Sequence $S$ starts:
2, 4, 5, 6, 8, ...
Instructions
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
- Sloane's A005228 and A030124.
- Wikipedia: Hofstadter Figure-Figure sequences.
Tests
tests:
- text: <code>ffr</code> is a function.
testString: assert(typeof ffr === 'function');
- text: <code>ffs</code> is a function.
testString: assert(typeof ffs === 'function');
- text: <code>ffr</code> should return integer.
testString: assert(Number.isInteger(ffr(1)));
- text: <code>ffs</code> should return integer.
testString: assert(Number.isInteger(ffs(1)));
- text: <code>ffr()</code> should return <code>69</code>
testString: assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1]);
- text: <code>ffr()</code> should return <code>1509</code>
testString: assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1]);
- text: <code>ffr()</code> should return <code>5764</code>
testString: assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1]);
- text: <code>ffr()</code> should return <code>526334</code>
testString: assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1]);
- text: <code>ffs()</code> should return <code>14</code>
testString: assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1]);
- text: <code>ffs()</code> should return <code>59</code>
testString: assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1]);
- text: <code>ffs()</code> should return <code>112</code>
testString: assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1]);
- text: <code>ffs()</code> should return <code>1041</code>
testString: assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1]);
Challenge Seed
// noprotect
function ffr(n) {
return n;
}
function ffs(n) {
return n;
}
After Test
const ffrParamRes = [[10, 69], [50, 1509], [100, 5764], [1000, 526334]];
const ffsParamRes = [[10, 14], [50, 59], [100, 112], [1000, 1041]];
Solution
// 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];
}