How many squares fit in a rectangle? I have a rectangle R with a given width W and a given height H. I need to put n equally-sized squares in that rectangle in an orderly way (not diagonally) so that the squares fit optimally in the rectangle filling out all the available space (of course, in most cases a small space will remain empty). What is the square edge-length E for any given number n of squares? ($E$ must be an integer number).
If $n=1$ then $E$ is the rectangle's shorter edge-length
But what about any other values of $n$?
I am sorry if this is a naive question for math experts, but it has been a  long time since I took math classes at school.
Illustrative example: I have a table $2$ meter $\times$ $1$ meter. I need to cut out $12$ pieces of square-sized paper to cover most of the surface of the table. What must be the edge length of each paper sheet to achieve this task?
 A: You want to factor $n$ into two factors that are close to the ratio of W to H.  This tells you how many squares are in each direction.  Then divide W by the number of squares in its dimension and H by the number in its dimension and take the smaller.
As an example, suppose $W=50, H=30$.  This gives $\frac WH = \frac 53$.  If there are $a$ squares in the W direction and $b$ squares in the H direction we have $ab=n$ and want $a \approx \frac 53b$, so we can look for $\frac 53b^2 \approx n, b \approx \sqrt {\frac 35n}$.  If $n=60$ we get $\sqrt {\frac35n}=\sqrt {36}=6$ and we can have $10 \times 6$.  The square size will be $\frac {50}{10}=5$.  This came out perfectly.  If we had $n=35$ we would have to take $a=7,b=5$ and the sizes would be $\frac {50}7=7\frac 17, \frac {30}5=6$.  As $6$ is smaller we use that and have $8$ units left in the W direction.
For your example, you can either lay out the squares $4 \times 3$ or $6 \times 2$.  For the first the $1$ m dimension is limiting and you get $\frac 13$ meter squares.  For the second the $2$ m dimension is limiting and you again get $\frac 26=\frac 13$ meter squares.  It is a coincidence of the numbers chosen that these match.
A: As shown in one answer and
another answer,
The maximum number of squares of edge-length $E$ that can fit in a rectangle
of width $W$ and height $H$ is
$$ \left\lfloor\frac WE\right\rfloor \times \left\lfloor\frac HE\right\rfloor, $$
and one way to fit that number of squares in the rectangle is to construct a grid of squares $\left\lfloor\frac WE\right\rfloor$ squares wide by
$\left\lfloor\frac HE\right\rfloor$ squares high in the lower left corner of the rectangle.
The proofs do not actually require any of these numbers to be an integer, but you may assume $E$ is an integer for the purpose of your question.
As mentioned in a comment (which could have been an answer), the edge length $E$
that you want is the largest integer such that
$\left\lfloor\frac WE\right\rfloor \times \left\lfloor\frac HE\right\rfloor \geq n.$
I do not see an obvious way to find the correct value of $E$ without allowing for some trial and error. But you can estimate the value to get the answer relatively quickly.
Let
$$ s = \sqrt{\frac{W\times H}{n}}. $$
Then $s$ is an upper bound on the possible length $E$; you cannot fit $n$ squares of side greater than $s$ in the rectangle, because the squares would have a greater area than the rectangle. Therefore $E \leq s.$
But we can do better than that, because we need whole rows and columns of squares inside the rectangle. If $s$ does not evenly divide both $W$ and $H$, the largest grid of whole squares of edge-length $s$ we can fit in the rectangle will not be enough squares.
The only way to get more squares is to reduce the edge length until we can fit at least one more row or one more column in the rectangle.
That is, we need either $\left\lceil H/s \right\rceil$ rows
or $\left\lceil W/s \right\rceil$ columns. So we conclude that
$$ E \leq
 \max\left\{\frac{W}{\left\lceil \frac Ws \right\rceil},
  \frac{H}{\left\lceil \frac Hs \right\rceil}\right\}
=  \max\left\{\frac{W}{\left\lceil \sqrt{\frac{n\times W}{H}}\, \right\rceil},
  \frac{H}{\left\lceil \sqrt{\frac{n\times H}{W}}\, \right\rceil}\right\}. $$
We can start at the largest integer less than or equal to the expression on the right, and if that value of $E$ is too large, try smaller values.

Example: Let $W=945$, $H=447$, and $n=34.$ Then
\begin{align}
\sqrt{\frac{n\times W}{H}} &\approx 8.48, \\
\sqrt{\frac{n\times H}{W}} &\approx 4.01, \\
\frac{W}{\left\lceil \sqrt{\frac{n\times W}{H}}\, \right\rceil}
&= \frac{945}{9} = 105, \\
\frac{H}{\left\lceil \sqrt{\frac{n\times H}{W}}\, \right\rceil}
&= \frac{447}{5} = 89.4. \\
\end{align}
The greater of the values $105$ and $89.4$ is $105.$
Trying $E = 105,$ we find we can fit $4$ rows by $9$ columns of squares of that size in the rectangle, and $4 \times 9 = 36,$ so we can omit two squares from the grid and we have fit $n=34$ squares in the rectangle.
Any larger square can fit at most $4$ rows by $8$ columns, which gives only $32$ squares, so $E=105$ is the correct answer.
