From 7faa1687616c4a52af4af7d6914db9e163b2c593 Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Mon, 24 May 2021 19:43:58 +0200 Subject: [PATCH] fix(curriculum): rework Project Euler 85 (#42181) * fix: rework challenge to use argument in function * fix: add solution * fix: grammar --- .../problem-85-counting-rectangles.md | 77 +++++++++++++++++-- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-85-counting-rectangles.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-85-counting-rectangles.md index 527ddd1ab7..5db9e13fec 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-85-counting-rectangles.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-85-counting-rectangles.md @@ -12,20 +12,44 @@ By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co a diagram of the different rectangles found within a 3 by 2 rectangular grid -Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the nearest solution. +Although there may not exists a rectangular grid that contains exactly `n` rectangles, find the area of the grid with the nearest solution. # --hints-- -`countingRectangles()` should return a number. +`countingRectangles(18)` should return a number. ```js -assert(typeof countingRectangles() === 'number'); +assert(typeof countingRectangles(18) === 'number'); ``` -`countingRectangles()` should return 2772. +`countingRectangles(18)` should return `6`. ```js -assert.strictEqual(countingRectangles(), 2772); +assert.strictEqual(countingRectangles(18), 6); +``` + +`countingRectangles(250)` should return `22`. + +```js +assert.strictEqual(countingRectangles(250), 22); +``` + +`countingRectangles(50000)` should return `364`. + +```js +assert.strictEqual(countingRectangles(50000), 364); +``` + +`countingRectangles(1000000)` should return `1632`. + +```js +assert.strictEqual(countingRectangles(1000000), 1632); +``` + +`countingRectangles(2000000)` should return `2772`. + +```js +assert.strictEqual(countingRectangles(2000000), 2772); ``` # --seed-- @@ -33,16 +57,53 @@ assert.strictEqual(countingRectangles(), 2772); ## --seed-contents-- ```js -function countingRectangles() { +function countingRectangles(n) { return true; } -countingRectangles(); +countingRectangles(18); ``` # --solutions-- ```js -// solution required +function countingRectangles(n) { + function numberOfRectangles(h, w) { + return (h * (h + 1) * w * (w + 1)) / 4; + } + + function rectangleArea(h, w) { + return h * w; + } + + let rectanglesCount = 1; + let maxSide = 1; + while (rectanglesCount < n) { + maxSide++; + rectanglesCount = numberOfRectangles(maxSide, 1); + } + + let bestDiff = Math.abs(rectanglesCount - n); + let bestSize = [maxSide, 1]; + + let curHeight = maxSide - 1; + let curWidth = 1; + + for (curWidth; curWidth < curHeight; curWidth++) { + for (curHeight; curHeight > curWidth; curHeight--) { + rectanglesCount = numberOfRectangles(curHeight, curWidth); + const curDiff = Math.abs(rectanglesCount - n); + if (curDiff < bestDiff) { + bestDiff = curDiff; + bestSize = [curHeight, curWidth]; + } + + if (rectanglesCount < n) { + break; + } + } + } + return rectangleArea(...bestSize); +} ```