Largest N squares that fit in a rectangle
Let

\begin{align}
C&amp;=\left\lfloor\frac WL\right\rfloor&amp;
R&amp;=\left\lfloor\frac HL\right\rfloor
\end{align}

be the maximum number of rows and columns respectively. It sounds as though you're looking for the maximum $L^*$ such that, when $L=L^*$, we have $CR\ge N$. Let

\begin{align}
L_0&amp;=\sqrt{\frac{WH}N}\\
C_0&amp;=\left\lceil\frac W{L_0}\right\rceil=\left\lceil\sqrt{\frac{WN}H}\right\rceil&amp;
R_0&amp;=\left\lceil\frac H{L_0}\right\rceil=\left\lceil\sqrt{\frac{HN}W}\right\rceil.
\end{align}

Observe that $L\le\min{\{W/C_0,H/R_0\}}$ is sufficient, and $L\le L_0$ is necessary:

\begin{align}
\frac{WH}{L^2}&amp;\ge\left\lfloor\frac WL\right\rfloor\left\lfloor\frac HL\right\rfloor=CR\ge N\\
L&amp;\le\sqrt{\frac{WH}N}=L_0,
\end{align}

with equality only if $W/L_0$ and $H/L_0$ both are integers. As a result, the optimal solution has dimension either $C_0\times\lceil N/C_0\rceil$ or $\lceil N/R_0\rceil\times R_0$.