curriculum/challenges/english/blocks/project-euler-problems-1-to-100/5900f3c11000cf542c50fed4.md
By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:
Although there may not exists a rectangular grid that contains exactly n rectangles, find the area of the grid with the nearest solution.
countingRectangles(18) should return a number.
assert(typeof countingRectangles(18) === 'number');
countingRectangles(18) should return 6.
assert.strictEqual(countingRectangles(18), 6);
countingRectangles(250) should return 22.
assert.strictEqual(countingRectangles(250), 22);
countingRectangles(50000) should return 364.
assert.strictEqual(countingRectangles(50000), 364);
countingRectangles(1000000) should return 1632.
assert.strictEqual(countingRectangles(1000000), 1632);
countingRectangles(2000000) should return 2772.
assert.strictEqual(countingRectangles(2000000), 2772);
function countingRectangles(n) {
return true;
}
countingRectangles(18);
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);
}