2018-09-30 22:01:58 +00:00
---
id: 5900f3c11000cf542c50fed4
title: 'Problem 85: Counting rectangles'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302199
2021-01-13 02:31:00 +00:00
dashedName: problem-85-counting-rectangles
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-02-28 12:39:47 +00:00
2018-09-30 22:01:58 +00:00
By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:
2020-02-28 12:39:47 +00:00
< img class = "img-responsive center-block" alt = "a diagram of the different rectangles found within a 3 by 2 rectangular grid" src = "https://cdn-media-1.freecodecamp.org/project-euler/counting-rectangles.png" style = "background-color: white; padding: 10px;" >
2018-09-30 22:01:58 +00:00
2021-05-24 17:43:58 +00:00
Although there may not exists a rectangular grid that contains exactly `n` rectangles, find the area of the grid with the nearest solution.
2020-02-28 12:39:47 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-05-24 17:43:58 +00:00
`countingRectangles(18)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-24 17:43:58 +00:00
assert(typeof countingRectangles(18) === 'number');
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-05-24 17:43:58 +00:00
`countingRectangles(18)` should return `6` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-24 17:43:58 +00:00
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);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-05-24 17:43:58 +00:00
function countingRectangles(n) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-05-24 17:43:58 +00:00
countingRectangles(18);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-05-24 17:43:58 +00:00
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);
}
2018-09-30 22:01:58 +00:00
```