From 42ff4307bafdd29c186623407041dc502fae7d31 Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Mon, 31 May 2021 19:34:05 +0200 Subject: [PATCH] fix(curriculum): rework Project Euler 94 (#42243) Co-authored-by: Sem Bauke <46919888+Sembauke@users.noreply.github.com> --- ...problem-94-almost-equilateral-triangles.md | 54 +++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-94-almost-equilateral-triangles.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-94-almost-equilateral-triangles.md index fbd3f04a8b..ef0c095e03 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-94-almost-equilateral-triangles.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-94-almost-equilateral-triangles.md @@ -8,24 +8,42 @@ dashedName: problem-94-almost-equilateral-triangles # --description-- -It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square units. +It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square units. We shall define an almost equilateral triangle to be a triangle for which two sides are equal and the third differs by no more than one unit. -Find the sum of the perimeters of all almost equilateral triangle with integral side lengths and area and whose perimeters do not exceed one billion (1,000,000,000). +Find the sum of the perimeters of all almost equilateral triangles with integral side lengths and area and whose perimeters do not exceed `limit`. # --hints-- -`almostEquilateralTriangles()` should return a number. +`almostEquilateralTriangles(50)` should return a number. ```js -assert(typeof almostEquilateralTriangles() === 'number'); +assert(typeof almostEquilateralTriangles(50) === 'number'); ``` -`almostEquilateralTriangles()` should return 518408346. +`almostEquilateralTriangles(50)` should return `66`. ```js -assert.strictEqual(almostEquilateralTriangles(), 518408346); +assert.strictEqual(almostEquilateralTriangles(50), 66); +``` + +`almostEquilateralTriangles(10000)` should return `3688`. + +```js +assert.strictEqual(almostEquilateralTriangles(10000), 3688); +``` + +`almostEquilateralTriangles(10000000)` should return `9973078`. + +```js +assert.strictEqual(almostEquilateralTriangles(10000000), 9973078); +``` + +`almostEquilateralTriangles(1000000000)` should return `518408346`. + +```js +assert.strictEqual(almostEquilateralTriangles(1000000000), 518408346); ``` # --seed-- @@ -33,16 +51,34 @@ assert.strictEqual(almostEquilateralTriangles(), 518408346); ## --seed-contents-- ```js -function almostEquilateralTriangles() { +function almostEquilateralTriangles(limit) { return true; } -almostEquilateralTriangles(); +almostEquilateralTriangles(50); ``` # --solutions-- ```js -// solution required +function almostEquilateralTriangles(limit) { + // Based on https://blog.dreamshire.com/project-euler-94-solution/ + let perimetersSum = 0; + + let sidesAB = 1; + let sideC = 1; + let perimeter = 0; + let perimeterOffset = 1; + + while (perimeter <= limit) { + [sidesAB, sideC] = [4 * sidesAB - sideC + 2 * perimeterOffset, sidesAB]; + perimeterOffset = -perimeterOffset; + + perimetersSum += perimeter; + perimeter = 3 * sidesAB - perimeterOffset; + } + + return perimetersSum; +} ```