2018-09-30 22:01:58 +00:00
---
id: 5900f3b31000cf542c50fec6
title: 'Problem 71: Ordered fractions'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302184
2021-01-13 02:31:00 +00:00
dashedName: problem-71-ordered-fractions
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-02-28 12:39:47 +00:00
2021-05-11 11:33:29 +00:00
Consider the fraction, $\frac{n}{d}$, where `n` and `d` are positive integers. If `n` < `d` and highest common factor, ${{HCF}(n, d)} = 1$, it is called a reduced proper fraction.
2020-02-28 12:39:47 +00:00
2020-11-27 18:02:05 +00:00
If we list the set of reduced proper fractions for `d` ≤ 8 in ascending order of size, we get:
2020-02-28 12:39:47 +00:00
2021-05-11 11:33:29 +00:00
$$\frac{1}{8}, \frac{1}{7}, \frac{1}{6}, \frac{1}{5}, \frac{1}{4}, \frac{2}{7}, \frac{1}{3}, \frac{3}{8}, \frac{\textbf2}{\textbf5}, \frac{3}{7}, \frac{1}{2}, \frac{4}{7}, \frac{3}{5}, \frac{5}{8}, \frac{2}{3}, \frac{5}{7}, \frac{3}{4}, \frac{4}{5}, \frac{5}{6}, \frac{6}{7}, \frac{7}{8}$$
2020-02-28 12:39:47 +00:00
2021-05-11 11:33:29 +00:00
It can be seen that $\frac{2}{5}$ is the fraction immediately to the left of $\frac{3}{7}$.
2020-02-28 12:39:47 +00:00
2021-05-11 11:33:29 +00:00
By listing the set of reduced proper fractions for `d` ≤ `limit` in ascending order of size, find the numerator of the fraction immediately to the left of $\frac{3}{7}$.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-05-11 11:33:29 +00:00
`orderedFractions(8)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-11 11:33:29 +00:00
assert(typeof orderedFractions(8) === 'number');
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-05-11 11:33:29 +00:00
`orderedFractions(8)` should return `2` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-11 11:33:29 +00:00
assert.strictEqual(orderedFractions(8), 2);
```
`orderedFractions(10)` should return `2` .
```js
assert.strictEqual(orderedFractions(10), 2);
```
`orderedFractions(9994)` should return `4283` .
```js
assert.strictEqual(orderedFractions(9994), 4283);
```
`orderedFractions(500000)` should return `214283` .
```js
assert.strictEqual(orderedFractions(500000), 214283);
```
`orderedFractions(1000000)` should return `428570` .
```js
assert.strictEqual(orderedFractions(1000000), 428570);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-05-11 11:33:29 +00:00
function orderedFractions(limit) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-05-11 11:33:29 +00:00
orderedFractions(8);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-05-11 11:33:29 +00:00
function orderedFractions(limit) {
const fractions = [];
const fractionValues = {};
const highBoundary = 3 / 7;
let lowBoundary = 2 / 7;
for (let denominator = limit; denominator > 2; denominator--) {
let numerator = Math.floor((3 * denominator - 1) / 7);
let value = numerator / denominator;
if (value > highBoundary || value < lowBoundary ) {
continue;
}
fractionValues[value] = [numerator, denominator];
fractions.push(value);
lowBoundary = value;
}
fractions.sort();
return fractionValues[fractions[fractions.length - 1]][0];
}
2018-09-30 22:01:58 +00:00
```