fix(curriculum): rework Project Euler 73 (#42300)
* fix: rework challenge to use argument in function * fix: add solution * fix: use MathJax to improve math notation look
This commit is contained in:
@ -8,28 +8,46 @@ dashedName: problem-73-counting-fractions-in-a-range
|
|||||||
|
|
||||||
# --description--
|
# --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:
|
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, <strong>3/8</strong>, <strong>2/5</strong>, <strong>3/7</strong>, 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}, \mathbf{\frac{3}{8}, \frac{2}{5}, \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 there are 3 fractions between 1/3 and 1/2.
|
It can be seen that there are `3` fractions between $\frac{1}{3}$ and $\frac{1}{2}$.
|
||||||
|
|
||||||
How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for `d` ≤ 12,000?
|
How many fractions lie between $\frac{1}{3}$ and $\frac{1}{2}$ in the sorted set of reduced proper fractions for `d` ≤ `limit`?
|
||||||
|
|
||||||
# --hints--
|
# --hints--
|
||||||
|
|
||||||
`countingFractionsInARange()` should return a number.
|
`countingFractionsInARange(8)` should return a number.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert(typeof countingFractionsInARange() === 'number');
|
assert(typeof countingFractionsInARange(8) === 'number');
|
||||||
```
|
```
|
||||||
|
|
||||||
`countingFractionsInARange()` should return 7295372.
|
`countingFractionsInARange(8)` should return `3`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.strictEqual(countingFractionsInARange(), 7295372);
|
assert.strictEqual(countingFractionsInARange(8), 3);
|
||||||
|
```
|
||||||
|
|
||||||
|
`countingFractionsInARange(1000)` should return `50695`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(countingFractionsInARange(1000), 50695);
|
||||||
|
```
|
||||||
|
|
||||||
|
`countingFractionsInARange(6000)` should return `1823861`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(countingFractionsInARange(6000), 1823861);
|
||||||
|
```
|
||||||
|
|
||||||
|
`countingFractionsInARange(12000)` should return `7295372`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(countingFractionsInARange(12000), 7295372);
|
||||||
```
|
```
|
||||||
|
|
||||||
# --seed--
|
# --seed--
|
||||||
@ -37,16 +55,29 @@ assert.strictEqual(countingFractionsInARange(), 7295372);
|
|||||||
## --seed-contents--
|
## --seed-contents--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function countingFractionsInARange() {
|
function countingFractionsInARange(limit) {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
countingFractionsInARange();
|
countingFractionsInARange(8);
|
||||||
```
|
```
|
||||||
|
|
||||||
# --solutions--
|
# --solutions--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// solution required
|
function countingFractionsInARange(limit) {
|
||||||
|
let result = 0;
|
||||||
|
const stack = [[3, 2]];
|
||||||
|
while (stack.length > 0) {
|
||||||
|
const [startDenominator, endDenominator] = stack.pop();
|
||||||
|
const curDenominator = startDenominator + endDenominator;
|
||||||
|
if (curDenominator <= limit) {
|
||||||
|
result++;
|
||||||
|
stack.push([startDenominator, curDenominator]);
|
||||||
|
stack.push([curDenominator, endDenominator]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user