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.
Task: Implement the Hofstadter Q Sequence equation into JavaScripthofstadterQ
is a function.
testString: 'assert(typeof hofstadterQ === "function", "hofstadterQ
is a function.");'
- text: hofstadterQ()
should return integer
testString: 'assert(Number.isInteger(hofstadterQ(1000)), "hofstadterQ()
should return integer
");'
- text: hofstadterQ(1000)
should return 502
testString: 'assert.equal(hofstadterQ(testCase[0]), res[0], "hofstadterQ(1000)
should return 502
");'
- text: hofstadterQ(1500)
should return 755
testString: 'assert.equal(hofstadterQ(testCase[1]), res[1], "hofstadterQ(1500)
should return 755
");'
- text: hofstadterQ(2000)
should return 1005
testString: 'assert.equal(hofstadterQ(testCase[2]), res[2], "hofstadterQ(2000)
should return 1005
");'
- text: hofstadterQ(2500)
should return 1261
testString: 'assert.equal(hofstadterQ(testCase[3]), res[3], "hofstadterQ(2500)
should return 1261
");'
```