Filling the grid

A rigorous proof

Given a w×hR+w \times h \in \mathbb{R}^{+} container and nNn \in \mathbb{N} squares find the maximum square side length sR+s \in \mathbb{R}^{+} for which a grid b×aNb \times a \in \mathbb{N} exists that fits the squares and fits in the container.

Let r=whr = \frac{w}{h} the ratio of the container’s dimensions and the sets:

W={(a,b)N2:nabarb}H={(a,b)N2:nabarb}\begin{gather*} W = \left\{ (a, b) \in \mathbb{N}^2 : n \le ab \land a \ge rb \right\} \\ H = \left\{ (a, b) \in \mathbb{N}^2 : n \le ab \land a \le rb \right\} \end{gather*}

Claim: The optimal solution for the side length of the squares is:

s=max1bnmin{wnb,hb}.s = \max_{\mathclap{1 \le b \le n}} \, \min \left\{ \frac{w}{\left\lceil \frac{n}{b} \right\rceil}, \frac{h}{b} \right\}.

Proof: WH={(a,b)N2:nab}W \cup H = \left\{ (a, b) \in \mathbb{N}^2 : n \le ab \right\}, all possible grid configurations.
For every (a=nb,b)(a = \left\lceil \frac{n}{b} \right\rceil, b) with bnb \le n let s=min{wa,hb}s = \min \left\{ \frac{w}{a}, \frac{h}{b} \right\}. Clearly, nabn \le ab always, hence s=wahb    arb    (a,b)Ws = \frac{w}{a} \le \frac{h}{b} \iff a \ge rb \iff (a, b) \in W and similarly s=hb    (a,b)Hs = \frac{h}{b} \iff (a, b) \in H. Moreover, aa is the minimal choice such that (a,b)WH(a, b) \in W \cup H.
Any a>aa' > a gives s=min{wa,hb}min{wa,hb}=ss' = \min \left\{ \frac{w}{a'}, \frac{h}{b} \right\} \le \min \left\{ \frac{w}{a}, \frac{h}{b} \right\} = s. Any b>nb' > n gives smin{w,hn}s' \le \min \left\{ w, \frac{h}{n} \right\}, which is ss for (a=1,b=n)(a = 1, b = n), therefore sss' \le s for some ss with bn.  b \le n. \thickspace \blacksquare

Claim: If (a=nb,b)WH(a = \left\lceil \frac{n}{b} \right\rceil, b) \in W \cup H maximizes ss then bbb \ge \lfloor b^* \rfloor, where b=nrb^* = \sqrt{\frac{n}{r}}.
Proof: For bbb \le b^*:

bnr    rbnba    hbwa    s=wnb.{b \le \sqrt{\frac{n}{r}} \implies rb \le \frac{n}{b} \le a} \implies {\frac{h}{b} \ge \frac{w}{a} \implies s = \frac{w}{\left\lceil \frac{n}{b} \right\rceil}}.

To maximize ss, either b>bb > b^* or b=b.  b = \lfloor b^* \rfloor. \thickspace \blacksquare

Claim: For r1r \ge 1, if (a=nb,b)WH\left(a = \left\lceil \frac{n}{b} \right\rceil, b\right) \in W \cup H maximizes ss then bbb \le \lceil b^* \rceil.
Proof: For bbb \ge b^*:

bnr    rbnb    rb>nb(1)nbrb(2).{b \ge \sqrt{\frac{n}{r}} \implies rb \ge \frac{n}{b}} \implies {\underbrace{rb > \left\lceil \frac{n}{b} \right\rceil}_{\text{(1)}} \lor \underbrace{\left\lceil \frac{n}{b} \right\rceil \ge rb}_{\text{(2)}}}.

If case (1)\text{(1)} is true, under the bbb \ge b^* constraint only b=bb = \lceil b^* \rceil maximizes ss, since:

rb>a    hb<wa    s=hb.{rb > a \implies \frac{h}{b} < \frac{w}{a} \implies s = \frac{h}{b}}.

If case (2)\text{(2)} is true, assuming r1r \ge 1:

rbnb<nb+1    rb2bn<0    b<12r+14r2+nr    b<12+(12+nr)2=b+1\begin{align*} \brk[3,]{&}rb \le \left\lceil \frac{n}{b} \right\rceil < \frac{n}{b} + 1 \brk[3,] \implies &rb^2 - b - n < 0 \brk \implies \brk[1,]{&}b < \frac{1}{2r}+\sqrt{\frac{1}{4r^2} + \frac{n}{r}} \\ \implies &b < \frac{1}{2} + \sqrt{\left(\frac{1}{2} + \sqrt{\frac{n}{r}}\right)^2} \brk[,1]{=} \brk[2,]{\\ \implies &b <} b^* + 1 \end{align*}

This means bb<b+1b^* \le b < b^* + 1 which forces b=bb = \lceil b^* \rceil. In conclusion, either b<bb < b^* or b=b.  b = \lceil b^* \rceil. \thickspace \blacksquare

Claim: For r1r \ge 1 the optimal side length is:

s=maxbbbb>0min{wnb,hb}s = \max_{\mathclap{\substack{\lfloor b^* \rfloor \le b \le \lceil b^* \rceil \\[0.18em] b > 0}}} \, \min \left\{\frac{w}{\left\lceil \frac{n}{b} \right\rceil}, \frac{h}{b} \right\}

For r1r \le 1 solve for r=1rr' = \frac{1}{r}. This procedure finds solutions for every r(0,)r \in (0, \infty).

Proof: For r1r \ge 1, the first formula, proven to always find the maximal ss, is simply restricted to b{b,b}b \in \{\lfloor b^* \rfloor, \lceil b^* \rceil\}, proven to always maximize ss.

For r1r \le 1, let sw,h(a,b)=min{wa,hb}s_{w,h}(a, b) = \min \left\{ \frac{w}{a}, \frac{h}{b} \right\} the side length of (a,b)WH(a, b) \in W \cup H. Let r=hw=1r1r' = \frac{h}{w} = \frac{1}{r} \ge 1, WW' and HH' defined as WW and HH but with rr' substituted for rr. sh,ws_{h,w} is the side length of (a,b)WH(a, b) \in W' \cup H'. Finally, let the symmetry function ϕ(a,b)=(b,a)\phi(a, b) = (b, a). It follows that:

(a,b)W    ϕ(a,b)H(a,b)H    ϕ(a,b)W\begin{gather*} (a, b) \in W \iff \phi(a, b) \in H' \\ (a, b) \in H \iff \phi(a, b) \in W' \end{gather*}

sw,h(a,b)=min{wa,hb}=min{hb,wa}=sh,w(b,a)=sh,w(ϕ(a,b)).s_{w,h}(a, b) = {\min \left\{ \frac{w}{a}, \frac{h}{b} \right\} = \min \left\{ \frac{h}{b}, \frac{w}{a} \right\}} = {s_{h,w}(b, a) = s_{h,w}(\phi(a, b))}.

ϕ\phi bijectively maps WW to HH' and HH to WW' while preserving side lengths. Therefore the solution for rr can be searched in the solution space for rr', and the solutions for rr and rr' are equal. The mapping r1rr \mapsto \frac{1}{r} is bijective on (0,)(0, \infty), thus this procedure finds all solutions. \blacksquare

>