2018-12-30 19:05:56 +05:30
---
id: 5a23c84252665b21eecc8028
title: Stern-Brocot sequence
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302324
2018-12-30 19:05:56 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'description' >
2019-05-22 23:30:29 +09:00
For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the < a href = "https://rosettacode.org/wiki/Fibonacci sequence" target = "_blank" > Fibonacci sequence< / a > .
2018-12-30 19:05:56 +05:30
< ol >
< li > The first and second members of the sequence are both 1:< / li >
< ul > < li > 1, 1< / li > < / ul >
< li > Start by considering the second member of the sequence< / li >
< li > Sum the considered member of the sequence and its precedent, (1 + 1) = 2, and append it to the end of the
sequence:< / li >
< ul > < li > 1, 1, 2< / li > < / ul >
< li > Append the considered member of the sequence to the end of the sequence:< / li >
< ul > < li > 1, 1, 2, 1< / li > < / ul >
< li > Consider the next member of the series, (the third member i.e. 2)< / li >
< li > GOTO 3 < / li >
< ul >
< li > < / li >
< li > ─── Expanding another loop we get: ───< / li >
< li > < / li >
< / ul >
< li > Sum the considered member of the sequence and its precedent, (2 + 1) = 3, and append it to the end of the
sequence:< / li >
< ul > < li > 1, 1, 2, 1, 3< / li > < / ul >
< li > Append the considered member of the sequence to the end of the sequence:< / li >
< ul > < li > 1, 1, 2, 1, 3, 2< / li > < / ul >
< li > Consider the next member of the series, (the fourth member i.e. 1)< / li >
< / ol >
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'instructions' >
2019-03-10 19:14:48 +09:00
Create a function that returns the $ n^{th} $ member of the sequence using the method outlined above.
2018-12-30 19:05:56 +05:30
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2018-12-30 19:05:56 +05:30
tests:
- text: < code > sternBrocot</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof sternBrocot == 'function');
2018-12-30 19:05:56 +05:30
- text: < code > sternBrocot(2)</ code > should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof sternBrocot(2) == 'number');
2018-12-30 19:05:56 +05:30
- text: < code > sternBrocot(2)</ code > should return < code > 3</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(sternBrocot(2), 3);
2018-12-30 19:05:56 +05:30
- text: < code > sternBrocot(3)</ code > should return < code > 5</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(sternBrocot(3), 5);
2018-12-30 19:05:56 +05:30
- text: < code > sternBrocot(5)</ code > should return < code > 11</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(sternBrocot(5), 11);
2018-12-30 19:05:56 +05:30
- text: < code > sternBrocot(7)</ code > should return < code > 19</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(sternBrocot(7), 19);
2018-12-30 19:05:56 +05:30
- text: < code > sternBrocot(10)</ code > should return < code > 39</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(sternBrocot(10), 39);
2018-12-30 19:05:56 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-10 19:14:48 +09:00
function sternBrocot(num) {
2020-09-15 09:57:40 -07:00
2018-12-30 19:05:56 +05:30
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'solution' >
```js
2019-03-10 19:14:48 +09:00
function sternBrocot(num) {
2018-12-30 19:05:56 +05:30
function f(n) {
2020-03-30 11:23:18 -05:00
return n < 2
? n
: n & 1
? f(Math.floor(n / 2)) + f(Math.floor(n / 2 + 1))
: f(Math.floor(n / 2));
2018-12-30 19:05:56 +05:30
}
function gcd(a, b) {
2020-03-30 11:23:18 -05:00
return a ? (a < b ? gcd ( b % a , a ) : gcd ( a % b , b ) ) : b ;
2018-12-30 19:05:56 +05:30
}
var n;
for (n = 1; f(n) != num; n++);
return n;
}
```
< / section >