2018-09-30 23:01:58 +01:00
---
title: Farey sequence
id: 59c3ec9f15068017c96eb8a3
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302266
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-06-14 20:04:16 +09:00
The < a href = "https://en.wikipedia.org/wiki/Farey sequence" title = "wp: Farey sequence" target = "_blank" > Farey sequence< / a > < code > F< sub > n< / sub > < / code > of order < code > n< / code > is the sequence of completely reduced fractions between < code > 0< / code > and < code > 1< / code > which, when in lowest terms, have denominators less than or equal to < code > n< / code > , arranged in order of increasing size.
2019-03-02 21:27:55 +09:00
The < i > Farey sequence< / i > is sometimes incorrectly called a < i > Farey series< / i > .
Each Farey sequence:
< ul >
< li > starts with the value 0, denoted by the fraction $ \frac{0}{1} $</ li >
< li > ends with the value 1, denoted by the fraction $ \frac{1}{1}$.</ li >
< / ul >
2019-06-14 20:04:16 +09:00
The Farey sequences of orders < code > 1< / code > to < code > 5< / code > are:
2019-03-02 21:27:55 +09:00
< ul >
< li style = "list-style: none;" > ${\bf\it{F}}_1 = \frac{0}{1}, \frac{1}{1}$</ li >
< li style = "list-style: none;" > ${\bf\it{F}}_2 = \frac{0}{1}, \frac{1}{2}, \frac{1}{1}$</ li >
< li style = "list-style: none;" > ${\bf\it{F}}_3 = \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}$</ li >
< li style = "list-style: none;" > ${\bf\it{F}}_4 = \frac{0}{1}, \frac{1}{4}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{3}{4}, \frac{1}{1}$</ li >
< li style = "list-style: none;" > ${\bf\it{F}}_5 = \frac{0}{1}, \frac{1}{5}, \frac{1}{4}, \frac{1}{3}, \frac{2}{5}, \frac{1}{2}, \frac{3}{5}, \frac{2}{3}, \frac{3}{4}, \frac{4}{5}, \frac{1}{1}$</ li >
< / ul >
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-06-14 20:04:16 +09:00
Write a function that returns the Farey sequence of order < code > n< / code > . The function should have one parameter that is < code > n< / code > . It should return the sequence as an array.
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-20 07:01:31 -08:00
- text: < code > farey</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof farey === 'function');
2018-10-04 14:37:37 +01:00
- text: < code > farey(3)</ code > should return an array
2019-07-26 05:24:52 -07:00
testString: assert(Array.isArray(farey(3)));
2018-10-20 21:02:47 +03:00
- text: < code > farey(3)</ code > should return < code > ["1/3","1/2","2/3"]</ code >
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(farey(3), ["1/3","1/2","2/3"]);
2018-10-20 21:02:47 +03:00
- text: < code > farey(4)</ code > should return < code > ["1/4","1/3","1/2","2/4","2/3","3/4"]</ code >
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(farey(4), ["1/4","1/3","1/2","2/4","2/3","3/4"]);
2018-10-20 21:02:47 +03:00
- text: < code > farey(5)</ code > should return < code > ["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]</ code >
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(farey(5), ["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-02 21:27:55 +09:00
function farey(n) {
2020-09-15 09:57:40 -07:00
2018-09-30 23:01:58 +01:00
}
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function farey(n){
let farSeq=[];
for(let den = 1; den < = n; den++){
for(let num = 1; num < den ; num + + ) {
farSeq.push({
str:num+"/"+den,
val:num/den});
}
}
farSeq.sort(function(a,b){
return a.val-b.val;
});
farSeq=farSeq.map(function(a){
return a.str;
});
return farSeq;
}
```
< / section >