fix(curriculum): rework Project Euler 71 (#42006)
This commit is contained in:
@ -8,28 +8,52 @@ dashedName: problem-71-ordered-fractions
|
||||
|
||||
# --description--
|
||||
|
||||
Consider the fraction, `n`/`d`, where `n` and `d` are positive integers. If `n`<`d` and HCF(`n`,`d`)=1, it is called a reduced proper fraction.
|
||||
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.
|
||||
|
||||
If we list the set of reduced proper fractions for `d` ≤ 8 in ascending order of size, we get:
|
||||
|
||||
<div style='text-align: center;'>1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, <strong>2/5</strong>, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8</div>
|
||||
$$\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}$$
|
||||
|
||||
It can be seen that 2/5 is the fraction immediately to the left of 3/7.
|
||||
It can be seen that $\frac{2}{5}$ is the fraction immediately to the left of $\frac{3}{7}$.
|
||||
|
||||
By listing the set of reduced proper fractions for `d` ≤ 1,000,000 in ascending order of size, find the numerator of the fraction immediately to the left of 3/7.
|
||||
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}$.
|
||||
|
||||
# --hints--
|
||||
|
||||
`orderedFractions()` should return a number.
|
||||
`orderedFractions(8)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof orderedFractions() === 'number');
|
||||
assert(typeof orderedFractions(8) === 'number');
|
||||
```
|
||||
|
||||
`orderedFractions()` should return 428570.
|
||||
`orderedFractions(8)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(orderedFractions(), 428570);
|
||||
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);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@ -37,16 +61,35 @@ assert.strictEqual(orderedFractions(), 428570);
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function orderedFractions() {
|
||||
function orderedFractions(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
orderedFractions();
|
||||
orderedFractions(8);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
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];
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user