* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
59637c4d89f6786115efd814 | Hofstadter Q sequence | 5 | 302287 | hofstadter-q-sequence |
--description--
The Hofstadter Q sequence is defined as:
Q(1)=Q(2)=1, \\\\ Q(n)=Q\\big(n-Q(n-1)\\big)+Q\\big(n-Q(n-2)), \\quad n>2.
It is defined like the Fibonacci sequence, but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.
--instructions--
Implement the Hofstadter Q Sequence equation as a function. The function should accept number, n
, and return an integer.
--hints--
hofstadterQ
should be a function.
assert(typeof hofstadterQ === 'function');
hofstadterQ()
should return integer
assert(Number.isInteger(hofstadterQ(1000)));
hofstadterQ(1000)
should return 502
assert.equal(hofstadterQ(testCase[0]), res[0]);
hofstadterQ(1500)
should return 755
assert.equal(hofstadterQ(testCase[1]), res[1]);
hofstadterQ(2000)
should return 1005
assert.equal(hofstadterQ(testCase[2]), res[2]);
hofstadterQ(2500)
should return 1261
assert.equal(hofstadterQ(testCase[3]), res[3]);
--seed--
--after-user-code--
const testCase = [1000, 1500, 2000, 2500];
const res = [502, 755, 1005, 1261];
--seed-contents--
function hofstadterQ(n) {
return n;
}
--solutions--
function hofstadterQ (n) {
const memo = [1, 1, 1];
const Q = function (i) {
let result = memo[i];
if (typeof result !== 'number') {
result = Q(i - Q(i - 1)) + Q(i - Q(i - 2));
memo[i] = result;
}
return result;
};
return Q(n);
}