Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
2021-01-12 19:31:00 -07:00

2.0 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
59637c4d89f6786115efd814 Hofstadter Q序列 5 hofstadter-q-sequence

--description--

Hofstadter Q序列定义为:

$ Q1= Q2= 1\\ Qn= Q \ bignQn-1\ big+ Q \ bignQn-2\ quad n> 2. $

它定义为Fibonacci序列 ,但Fibonacci序列中的下一个术语是前两个术语的总和在Q序列中前两个术语告诉您在Q序列中返回多远以找到两个数字总结以制作序列的下一个术语。

任务将Hofstadter Q Sequence方程实现为JavaScript

--hints--

hofstadterQ是一个函数。

assert(typeof hofstadterQ === 'function');

hofstadterQ()应该返回integer

assert(Number.isInteger(hofstadterQ(1000)));

hofstadterQ(1000)应该返回502

assert.equal(hofstadterQ(testCase[0]), res[0]);

hofstadterQ(1500)应该返回755

assert.equal(hofstadterQ(testCase[1]), res[1]);

hofstadterQ(2000)应该返回1005

assert.equal(hofstadterQ(testCase[2]), res[2]);

hofstadterQ(2500)应该返回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);
}