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

1.2 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
5900f3881000cf542c50fe9b 问题28对角螺旋数 5 problem-28-number-spiral-diagonals

--description--

从数字1开始沿顺时针方向向右移动形成5 x 5螺旋如下所示

21 22 23 24 25

20 7 8 9 10

19 6 1 2 11

18 5 4 3 12

17 16 15 14 13

可以验证对角线上的数字之和为101。

以相同方式形成的n×n螺旋中的对角线上的数字的总和是多少

--hints--

spiralDiagonals(101)应该返回692101。

assert(spiralDiagonals(101) == 692101);

spiralDiagonals(101)应该返回18591725。

assert(spiralDiagonals(303) == 18591725);

spiralDiagonals(101)应该返回85986601。

assert(spiralDiagonals(505) == 85986601);

spiralDiagonals(101)应该返回669171001。

assert(spiralDiagonals(1001) == 669171001);

--seed--

--seed-contents--

function spiralDiagonals(n) {

  return n;
}

spiralDiagonals(1001);

--solutions--

const spiralDiagonals = (n) => {
  const Sn2 = (n) => {
    return n*(n+1)*(2*n+1)/6;
  };
  const Sn = (n) => {
    return n*(n+1)/2;
  };
  let sum = (Sn2(n-1) + Sn(n-1) + n-1) + (Math.floor(n/2) + Sn2(n));
  return sum;
};