Filling the grid

A rigorous proof

Given a w×h∈R+w \times h \in \mathbb{R}^{+} container and n∈Nn \in \mathbb{N} squares find the maximum square side length s∈R+s \in \mathbb{R}^{+} for which a grid b×a∈Nb \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:n≤ab∧a≥rb}H={(a,b)∈N2:n≤ab∧a≤rb}\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=max⁡1≤b≤n min⁡{w⌈nb⌉,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: W∪H={(a,b)∈N2:n≤ab}W \cup H = \left\{ (a, b) \in \mathbb{N}^2 : n \le ab \right\}, all possible grid configurations.
For every (a=⌈n/b⌉,b)(a = \left\lceil n/b \right\rceil, b) with b≤nb \le n let s=min⁡{ w/a,h/b }s = \min \{\, w/a, h/b \,\}. Clearly, n≤abn \le ab always, hence s=w/a≤h/b  ⟺  a≥rb  ⟺  (a,b)∈Ws = w/a \le h/b \iff a \ge rb \iff (a, b) \in W and similarly s=h/b  ⟺  (a,b)∈Hs = h/b \iff (a, b) \in H. Moreover, aa is the minimal choice such that (a,b)∈W∪H(a, b) \in W \cup H.
Any a′>aa' > a gives s′=min⁡{ w/a′,h/b }≤min⁡{ w/a,h/b }=ss' = \min \{\, w/a', h/b \,\} \le \min \{\, w/a, h/b \,\} = s. Any b′>nb' > n gives s′≤min⁡{ w,h/n }s' \le \min \{\, w, h/n \,\}, which is ss for (a=1,b=n)(a = 1, b = n), therefore s′≤ss' \le s for some ss with b≤n.  ■b \le n. \thickspace \blacksquare

Claim: If (a=⌈n/b⌉,b)∈W∪H(a = \left\lceil n/b \right\rceil, b) \in W \cup H maximizes ss then b≥⌊b∗⌋b \ge \lfloor b^* \rfloor, where b∗=n/rb^* = \sqrt{n/r}.
Proof: For b≤b∗b \le b^*:

b≤nr  ⟹  rb≤nb≤a  ⟹  hb≥wa  ⟹  s=w⌈nb⌉.{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>b∗b > b^* or b=⌊b∗⌋.  ■b = \lfloor b^* \rfloor. \thickspace \blacksquare

Claim: For r≥1r \ge 1, if (a=⌈n/b⌉,b)∈W∪H\left(a = \left\lceil n/b \right\rceil, b\right) \in W \cup H maximizes ss then b≤⌈b∗⌉b \le \lceil b^* \rceil.
Proof: For b≥b∗b \ge b^*:

b≥nr  ⟹  rb≥nb  ⟹  rb>⌈nb⌉⏟(1)∨⌈nb⌉≥rb⏟(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 b≥b∗b \ge b^* constraint only b=⌈b∗⌉b = \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 r≥1r \ge 1:

rb≤⌈nb⌉<nb+1  ⟹  rb2−b−n<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 b∗≤b<b∗+1b^* \le b < b^* + 1 which forces b=⌈b∗⌉b = \lceil b^* \rceil. In conclusion, either b<b∗b < b^* or b=⌈b∗⌉.  ■b = \lceil b^* \rceil. \thickspace \blacksquare

Claim: For r≥1r \ge 1 the optimal side length is:

s=max⁡⌊b∗⌋≤b≤⌈b∗⌉b>0 min⁡{w⌈nb⌉,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 r≤1r \le 1 solve for r′=1rr' = \frac{1}{r}. This procedure finds solutions for every r∈(0,∞)r \in (0, \infty).
Proof: For r≥1r \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 r≤1r \le 1, let sw,h(a,b)=min⁡{wa,hb}s_{w,h}(a, b) = \min \big\{ \frac{w}{a}, \frac{h}{b} \big\} the side length of (a,b)∈W∪H(a, b) \in W \cup H. Let r′=hw=1r≥1r' = \frac{h}{w} = \frac{1}{r} \ge 1, W′W' and H′H' defined as WW and HH but with r′r' substituted for rr. sh,ws_{h,w} is the side length of (a,b)∈W′∪H′(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 H′H' and HH to W′W' while preserving side lengths. Therefore the solution for rr can be searched in the solution space for r′r', and the solutions for rr and r′r' are equal. The mapping r↦1rr \mapsto \frac{1}{r} is bijective on (0,∞)(0, \infty), thus this procedure finds all solutions. ■\blacksquare

>