From f559f187473353585322759c3a8f2ccefd1d4946 Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Thu, 3 Jun 2021 07:00:28 +0200 Subject: [PATCH] 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 --- ...roblem-73-counting-fractions-in-a-range.md | 53 +++++++++++++++---- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-73-counting-fractions-in-a-range.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-73-counting-fractions-in-a-range.md index 68b3e0d702..10c1ae6ea8 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-73-counting-fractions-in-a-range.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-73-counting-fractions-in-a-range.md @@ -8,28 +8,46 @@ dashedName: problem-73-counting-fractions-in-a-range # --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: -
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
+$$\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-- -`countingFractionsInARange()` should return a number. +`countingFractionsInARange(8)` should return a number. ```js -assert(typeof countingFractionsInARange() === 'number'); +assert(typeof countingFractionsInARange(8) === 'number'); ``` -`countingFractionsInARange()` should return 7295372. +`countingFractionsInARange(8)` should return `3`. ```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-- @@ -37,16 +55,29 @@ assert.strictEqual(countingFractionsInARange(), 7295372); ## --seed-contents-- ```js -function countingFractionsInARange() { +function countingFractionsInARange(limit) { return true; } -countingFractionsInARange(); +countingFractionsInARange(8); ``` # --solutions-- ```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; +} ```