From c0aaecba634d707a49244ae27290f488c405dddc Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Tue, 1 Jun 2021 08:50:51 +0200 Subject: [PATCH] fix(curriculum): rework Project Euler 72 (#42282) * fix: rework challenge to use argument in function * fix: add solution * fix: use MathJax in fractions and equations * fix: missing backticks --- .../problem-72-counting-fractions.md | 60 +++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-72-counting-fractions.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-72-counting-fractions.md index ac66afa5a4..3929bde7af 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-72-counting-fractions.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-72-counting-fractions.md @@ -8,28 +8,46 @@ dashedName: problem-72-counting-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: -
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}, \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 21 elements in this set. +It can be seen that there are `21` elements in this set. -How many elements would be contained in the set of reduced proper fractions for `d` ≤ 1,000,000? +How many elements would be contained in the set of reduced proper fractions for `d` ≤ `limit`? # --hints-- -`countingFractions()` should return a number. +`countingFractions(8)` should return a number. ```js -assert(typeof countingFractions() === 'number'); +assert(typeof countingFractions(8) === 'number'); ``` -`countingFractions()` should return 303963552391. +`countingFractions(8)` should return `21`. ```js -assert.strictEqual(countingFractions(), 303963552391); +assert.strictEqual(countingFractions(8), 21); +``` + +`countingFractions(20000)` should return `121590395`. + +```js +assert.strictEqual(countingFractions(20000), 121590395); +``` + +`countingFractions(500000)` should return `75991039675`. + +```js +assert.strictEqual(countingFractions(500000), 75991039675); +``` + +`countingFractions(1000000)` should return `303963552391`. + +```js +assert.strictEqual(countingFractions(1000000), 303963552391); ``` # --seed-- @@ -37,16 +55,36 @@ assert.strictEqual(countingFractions(), 303963552391); ## --seed-contents-- ```js -function countingFractions() { +function countingFractions(limit) { return true; } -countingFractions(); +countingFractions(8); ``` # --solutions-- ```js -// solution required +function countingFractions(limit) { + const phi = {}; + let count = 0; + + for (let i = 2; i <= limit; i++) { + if (!phi[i]) { + phi[i] = i; + } + if (phi[i] === i) { + for (let j = i; j <= limit; j += i) { + if (!phi[j]) { + phi[j] = j; + } + phi[j] = (phi[j] / i) * (i - 1); + } + } + count += phi[i]; + } + + return count; +} ```