Given a w×h∈R+
container and n∈N
squares find the maximum square side length s∈R+
for which a grid b×a∈N
exists that fits the squares and fits in the container.
Let r=hw
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}
Claim: The optimal solution for the side length of
the squares is:
s=1≤b≤nmaxmin{⌈bn⌉w,bh}.
Proof:W∪H={(a,b)∈N2:n≤ab},
all possible grid configurations. For every (a=⌈bn⌉,b)
with b≤n
let s=min{aw,bh}.
Clearly, n≤ab
always, hence s=aw≤bh⟺a≥rb⟺(a,b)∈W
and similarly s=bh⟺(a,b)∈H.
Moreover, a
is the minimal choice such that (a,b)∈W∪H. Any
a′>a
gives s′=min{a′w,bh}≤min{aw,bh}=s.
Any b′>n
gives s′≤min{w,nh},
which is s
for (a=1,b=n),
therefore s′≤s
for some s
with b≤n.■
Claim: If (a=⌈bn⌉,b)∈W∪H
maximizes s
then b≥⌊b∗⌋,
where b∗=rn. Proof: For b≤b∗:
b≤rn⟹rb≤bn≤a⟹bh≥aw⟹s=⌈bn⌉w.
To maximize s,
either b>b∗
or b=⌊b∗⌋.■
Claim: For r≥1,
if (a=⌈bn⌉,b)∈W∪H
maximizes s
then b≤⌈b∗⌉. Proof: For b≥b∗:
b≥rn⟹rb≥bn⟹(1)rb>⌈bn⌉∨(2)⌈bn⌉≥rb.
If case (1)
is true, under the b≥b∗
constraint only b=⌈b∗⌉
maximizes s,
since:
This means b∗≤b<b∗+1
which forces b=⌈b∗⌉.
In conclusion, either b<b∗
or b=⌈b∗⌉.■
Claim: For r≥1
the optimal side length is:
s=⌊b∗⌋≤b≤⌈b∗⌉b>0maxmin{⌈bn⌉w,bh}
For r≤1
solve for r′=r1.
This procedure finds solutions for every r∈(0,∞).
Proof: For r≥1,
the first formula, proven to always find the maximal s,
is simply restricted to b∈{⌊b∗⌋,⌈b∗⌉},
proven to always maximize s.
For r≤1,
let sw,h(a,b)=min{aw,bh}
the side length of (a,b)∈W∪H.
Let r′=wh=r1≥1,W′
and H′
defined as W
and H
but with r′
substituted for r.sh,w
is the side length of (a,b)∈W′∪H′.
Finally, let the symmetry function ϕ(a,b)=(b,a).
It follows that:
ϕ
bijectively maps W
to H′
and H
to W′
while preserving side lengths. Therefore the solution for r
can be searched in the solution space for r′,
and the solutions for r
and r′
are equal. The mapping r↦r1
is bijective on (0,∞),
thus this procedure finds all solutions. ■