I need to pack $N$ identical squares arranged in array fashion, $R$ rows of $C$ columns, the last row possibly incomplete, in a $W\times H$ rectangle (reals). The squares must be as large as possible.
With $S$ the side of the squares, this leads to the system of inequations
$$\begin{cases}CS\le W,\\RS\le H,\\CR\ge N.\end{cases}$$
Multiplying the first two and dividing by the third,
$$S^2\le\frac{WH}N.$$
My first naïve approach was to compute $S$ by assuming that the equality holds and deduce
$$C=\left\lfloor\frac WS\right\rfloor,R=\left\lfloor\frac HS\right\rfloor.$$
But this is wrong as the third inequation can be invalidated.
My second approach is by trying every possible value of $C$ from $1$ to $N$, deducing $R$ from $CR\ge N$, then $S$. Hence, finding the $C$ that maximizes
$$\min_C\left(\frac WC,\frac H{\left\lceil\dfrac NC\right\rceil}\right)$$ or minimizes $$\max_C\left(\frac HWC,\left\lceil\dfrac NC\right\rceil\right).$$
I suspect that there should be a closed-form solution, probably close to my naïve estimate, but so far I haven't found a line of attack. Can you help ?
Update:
Following @DarthGeek, the solution is found where $\dfrac{\left\lceil\dfrac NC\right\rceil}C$ crosses $\dfrac HW=:a$. This function is piecewise linear and monotonic. As we are only interested in values at integers, a linear or dichotomic search can do.
From $$\frac NC\le\left\lceil\frac NC\right\rceil<\frac NC+1$$
we deduce that the solution will lie in $$\left[\sqrt{\frac{N}a},\sqrt{\frac{N}a+\frac1{4a^2}}+\frac1{2a}\right).$$