Project Euler #85: Find the rectangular grid with closest to 2M rectangles
Well, let's ignore the code, because having started coding at that stage is wrong.

Let's look instead at a better starting-point:


Find a closed formula for the number of rectangles in a grid of given size (x, y):

N = \$\sum_{a=1}^x\sum_{b=1}^y(x-a+1)(y-b+a)\$
\$= \sum_{a=0}^{x-1}\sum_{b=0}^{y-1}(x-a)(y-b)\$
\$ = \frac 1 4 * (2x^2 - x*(x-1))(2y^2 - y*(y-1))\$
\$= \frac 1 4 * (x^2+x)(y^2+y)\$
Solve it for given y and N:

\$x^2+x-\frac{4*N}{y^2+y} = 0\$
\$x=\frac{-1 \pm \sqrt{1 + \frac{4*4*N}{y^2+y}}}2\$
\$x=-\frac 1 2 \pm \sqrt{\frac 1 4 + \frac{4*N}{y^2+y}}\$

(Only the positive solution is of interest)
Simply iterate all possible rectangles starting with short side of 1 until our short dimension becomes the long one, and calculate the number of rectangles for a slightly bigger / smaller rectangle than the fractional one we calculated.


The solution will be found in \$\Theta\left(\sqrt[4]N\right)\$.