From 4260416ac3f39e7bb15eade6a764786ec31c6f55 Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Tue, 11 May 2021 14:56:58 +0200 Subject: [PATCH] fix(curriculum): rework Project Euler 75 (#42066) * fix: rework challenge to use argument in function * fix: add solution * fix: position block evenly between paragraphs --- ...lem-75-singular-integer-right-triangles.md | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-75-singular-integer-right-triangles.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-75-singular-integer-right-triangles.md index 4a71f87c72..a20c0a95ea 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-75-singular-integer-right-triangles.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-75-singular-integer-right-triangles.md @@ -16,29 +16,47 @@ It turns out that 12 cm is the smallest length of wire that can be bent to form 30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
- 48 cm: (12,16,20)
+ 48 cm: (12,16,20)

In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
- 120 cm: (30,40,50), (20,48,52), (24,45,51) + 120 cm: (30,40,50), (20,48,52), (24,45,51)

-Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed? +Given that L is the length of the wire, for how many values of L ≤ `n` can exactly one, integer sided right angle, triangle be formed? # --hints-- -`singularIntRightTriangles()` should return a number. +`singularIntRightTriangles(48)` should return a number. ```js -assert(typeof singularIntRightTriangles() === 'number'); +assert(typeof singularIntRightTriangles(48) === 'number'); ``` -`singularIntRightTriangles()` should return 161667. +`singularIntRightTriangles(48)` should return `6`. ```js -assert.strictEqual(singularIntRightTriangles(), 161667); +assert.strictEqual(singularIntRightTriangles(48), 6); +``` + +`singularIntRightTriangles(700000)` should return `75783`. + +```js +assert.strictEqual(singularIntRightTriangles(700000), 75783); +``` + +`singularIntRightTriangles(1000000)` should return `107876`. + +```js +assert.strictEqual(singularIntRightTriangles(1000000), 107876); +``` + +`singularIntRightTriangles(1500000)` should return `161667`. + +```js +assert.strictEqual(singularIntRightTriangles(1500000), 161667); ``` # --seed-- @@ -46,16 +64,54 @@ assert.strictEqual(singularIntRightTriangles(), 161667); ## --seed-contents-- ```js -function singularIntRightTriangles() { +function singularIntRightTriangles(n) { return true; } -singularIntRightTriangles(); +singularIntRightTriangles(48); ``` # --solutions-- ```js -// solution required +function singularIntRightTriangles(limit) { + function euclidFormula(m, n) { + return [m ** 2 - n ** 2, 2 * m * n, m ** 2 + n ** 2]; + } + + function gcd(numberA, numberB) { + if (numberB === 0) { + return numberA; + } + return gcd(numberB, numberA % numberB); + } + + function notBothOdd(numberA, numberB) { + return (numberA + numberB) % 2 === 1; + } + + function areCoprime(numberA, numberB) { + return gcd(numberA, numberB) === 1; + } + + const trianglesWithPerimeter = new Array(limit + 1).fill(0); + const mLimit = Math.sqrt(limit / 2); + + for (let m = 2; m < mLimit; m++) { + for (let n = 1; n < m; n++) { + if (notBothOdd(m, n) && areCoprime(m, n)) { + const [sideA, sideB, sideC] = euclidFormula(m, n); + const perimeter = sideA + sideB + sideC; + let curPerimeter = perimeter; + while (curPerimeter <= limit) { + trianglesWithPerimeter[curPerimeter]++; + curPerimeter += perimeter; + } + } + } + } + return trianglesWithPerimeter.filter(trianglesCount => trianglesCount === 1) + .length; +} ```