2018-10-10 22:03:03 +00:00
---
id: 59c3ec9f15068017c96eb8a3
2021-02-06 04:42:36 +00:00
title: Farey sequence
2018-10-10 22:03:03 +00:00
challengeType: 5
2021-02-06 04:42:36 +00:00
forumTopicId: 302266
2021-01-13 02:31:00 +00:00
dashedName: farey-sequence
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The [Farey sequence ](https://en.wikipedia.org/wiki/Farey sequence "wp: Farey sequence" ) < code > F< sub > n</ sub ></ code > of order `n` is the sequence of completely reduced fractions between `0` and `1` which, when in lowest terms, have denominators less than or equal to `n` , arranged in order of increasing size.
The *Farey sequence* is sometimes incorrectly called a *Farey series* .
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 >
The Farey sequences of orders `1` to `5` are:
< 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 >
# --instructions--
Write a function that returns the Farey sequence of order `n` . The function should have one parameter that is `n` . It should return the sequence as an array.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`farey` should be a function.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(typeof farey === 'function');
```
2021-02-06 04:42:36 +00:00
`farey(3)` should return an array
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(Array.isArray(farey(3)));
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`farey(3)` should return `["1/3","1/2","2/3"]`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.deepEqual(farey(3), ['1/3', '1/2', '2/3']);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`farey(4)` should return `["1/4","1/3","1/2","2/4","2/3","3/4"]`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.deepEqual(farey(4), ['1/4', '1/3', '1/2', '2/4', '2/3', '3/4']);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`farey(5)` should return `["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]`
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
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-10-10 22:03:03 +00:00
```
2020-08-13 15:24:35 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
function farey(n) {
}
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```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;
}
```