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.5 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a23c84252665b21eecc8028 Stern-Brocot sequence 5 302324 stern-brocot-sequence

--description--

For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the Fibonacci sequence.

  1. The first and second members of the sequence are both 1:
    • 1, 1
  2. Start by considering the second member of the sequence
  3. Sum the considered member of the sequence and its precedent, (1 + 1) = 2, and append it to the end of the sequence:
    • 1, 1, 2
  4. Append the considered member of the sequence to the end of the sequence:
    • 1, 1, 2, 1
  5. Consider the next member of the series, (the third member i.e. 2)
  6. GOTO 3
    • ─── Expanding another loop we get: ───
  7. Sum the considered member of the sequence and its precedent, (2 + 1) = 3, and append it to the end of the sequence:
    • 1, 1, 2, 1, 3
  8. Append the considered member of the sequence to the end of the sequence:
    • 1, 1, 2, 1, 3, 2
  9. Consider the next member of the series, (the fourth member i.e. 1)

--instructions--

Create a function that returns the n^{th} member of the sequence using the method outlined above.

--hints--

sternBrocot should be a function.

assert(typeof sternBrocot == 'function');

sternBrocot(2) should return a number.

assert(typeof sternBrocot(2) == 'number');

sternBrocot(2) should return 3.

assert.equal(sternBrocot(2), 3);

sternBrocot(3) should return 5.

assert.equal(sternBrocot(3), 5);

sternBrocot(5) should return 11.

assert.equal(sternBrocot(5), 11);

sternBrocot(7) should return 19.

assert.equal(sternBrocot(7), 19);

sternBrocot(10) should return 39.

assert.equal(sternBrocot(10), 39);

--seed--

--seed-contents--

function sternBrocot(num) {

}

--solutions--

function sternBrocot(num) {
  function f(n) {
    return n < 2
      ? n
      : n & 1
      ? f(Math.floor(n / 2)) + f(Math.floor(n / 2 + 1))
      : f(Math.floor(n / 2));
  }

  function gcd(a, b) {
    return a ? (a < b ? gcd(b % a, a) : gcd(a % b, b)) : b;
  }
  var n;
  for (n = 1; f(n) != num; n++);
  return n;
}