Files
freeCodeCamp/curriculum/challenges/ukrainian/10-coding-interview-prep/rosetta-code/farey-sequence.md
2023-01-30 18:58:54 +02:00

107 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 59c3ec9f15068017c96eb8a3
title: Ряд Фарея
challengeType: 1
forumTopicId: 302266
dashedName: farey-sequence
---
# --description--
The 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.
*Ряд Фарея* деколи помилково називають *Серією Фарея*.
Кожен ряд Фарея:
<ul>
<li>починається зі значення 0, вираженого дробом $ \frac{0}{1}$</li>
<li>закінчується значенням 1, вираженим дробом {1}{1}$.</li>
</ul>
Послідовностями рядів Фарея від `1` до `5` є:
<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--
Напишіть функцію, яка повертає послідовність ряду Фарея`n`. Функція повинна мати один параметр, який є`n`. Вона має повернути послідовність у вигляді масиву.
# --hints--
`farey` має бути функцією.
```js
assert(typeof farey === 'function');
```
`farey(3)` має повернути масив
```js
assert(Array.isArray(farey(3)));
```
`farey(3)` should return `['0/1','1/3','1/2','2/3','1/1']`
```js
assert.deepEqual(farey(3),['0/1', '1/3', '1/2', '2/3', '1/1']);
```
`farey(4)` should return `['0/1','1/4','1/3','1/2','2/3','3/4','1/1']`
```js
assert.deepEqual(farey(4), ['0/1', '1/4', '1/3', '1/2', '2/3', '3/4', '1/1']);
```
`farey(5)` should return `['0/1','1/5','1/4','1/3','2/5','1/2','3/5','2/3','3/4','4/5','1/1']`
```js
assert.deepEqual(farey(5), [
'0/1',
'1/5',
'1/4',
'1/3',
'2/5',
'1/2',
'3/5',
'2/3',
'3/4',
'4/5',
'1/1'
]);
```
# --seed--
## --seed-contents--
```js
function farey(n) {
}
```
# --solutions--
```js
function farey(n) {
const sequence = [{ string: "0/1", float: 0.0 }];
for (let i = 1; i < n; i++) {
for (let j = n; j >= i; j--) {
if (i === 1 || j % i > 0) {
sequence.push({ string: `${i}/${j}`, float: i / j });
}
}
}
return sequence
.sort((a, b) => a.float - b.float)
.map(e => e.string)
}
```