mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-11 16:00:12 -04:00
2.6 KiB
2.6 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 59c3ec9f15068017c96eb8a3 | 法里序列 | 1 | 302266 | farey-sequence |
--description--
The Farey sequence Fn 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.
Farey 序列 有时被错误地称为 Farey 系列。
每个 Farey 序列:
- starts with the value 0, denoted by the fraction $ \frac{0}{1} $
- ends with the value 1, denoted by the fraction $ \frac{1}{1}$.
Farey 序列的 1 阶到 5 阶是:
- ${\bf\it{F}}_1 = \frac{0}{1}, \frac{1}{1}$
- ${\bf\it{F}}_2 = \frac{0}{1}, \frac{1}{2}, \frac{1}{1}$
- ${\bf\it{F}}_3 = \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}$
- ${\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}$
- ${\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}$
--instructions--
编写一个返回 n 阶 Farey 序列的函数。 该函数应该有一个参数,即 n。 It should return the sequence as an array.
--hints--
farey 应该是一个函数。
assert(typeof farey === 'function');
farey(3) 应该返回一个数组
assert(Array.isArray(farey(3)));
farey(3) should return ['0/1','1/3','1/2','2/3','1/1']
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']
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']
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--
function farey(n) {
}
--solutions--
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)
}