How to calculate maximal square size when fitting N squares in a given container?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Given a container which width is W and height is H, I'd like to fit N squares of maximal size S in it.



Example 1



W = 240
H = 210
N = 7
S = 70


enter image description here



Example 2



W = 200
H = 230
N = 23
S = 40


enter image description here



How would you calculate S from W, H, and N in O(1)?







share|cite|improve this question



















  • Do the squares have to be axis aligned? That makes it much easier
    – Ross Millikan
    Jul 28 at 5:05










  • @RossMillikan Yes!
    – Misha Moroshko
    Jul 28 at 5:12










  • Does the original rectangle always have integer dimensions, and do the squares need to have an integer side length?
    – prubin
    Jul 29 at 21:59














up vote
0
down vote

favorite












Given a container which width is W and height is H, I'd like to fit N squares of maximal size S in it.



Example 1



W = 240
H = 210
N = 7
S = 70


enter image description here



Example 2



W = 200
H = 230
N = 23
S = 40


enter image description here



How would you calculate S from W, H, and N in O(1)?







share|cite|improve this question



















  • Do the squares have to be axis aligned? That makes it much easier
    – Ross Millikan
    Jul 28 at 5:05










  • @RossMillikan Yes!
    – Misha Moroshko
    Jul 28 at 5:12










  • Does the original rectangle always have integer dimensions, and do the squares need to have an integer side length?
    – prubin
    Jul 29 at 21:59












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Given a container which width is W and height is H, I'd like to fit N squares of maximal size S in it.



Example 1



W = 240
H = 210
N = 7
S = 70


enter image description here



Example 2



W = 200
H = 230
N = 23
S = 40


enter image description here



How would you calculate S from W, H, and N in O(1)?







share|cite|improve this question











Given a container which width is W and height is H, I'd like to fit N squares of maximal size S in it.



Example 1



W = 240
H = 210
N = 7
S = 70


enter image description here



Example 2



W = 200
H = 230
N = 23
S = 40


enter image description here



How would you calculate S from W, H, and N in O(1)?









share|cite|improve this question










share|cite|improve this question




share|cite|improve this question









asked Jul 28 at 4:51









Misha Moroshko

1012




1012











  • Do the squares have to be axis aligned? That makes it much easier
    – Ross Millikan
    Jul 28 at 5:05










  • @RossMillikan Yes!
    – Misha Moroshko
    Jul 28 at 5:12










  • Does the original rectangle always have integer dimensions, and do the squares need to have an integer side length?
    – prubin
    Jul 29 at 21:59
















  • Do the squares have to be axis aligned? That makes it much easier
    – Ross Millikan
    Jul 28 at 5:05










  • @RossMillikan Yes!
    – Misha Moroshko
    Jul 28 at 5:12










  • Does the original rectangle always have integer dimensions, and do the squares need to have an integer side length?
    – prubin
    Jul 29 at 21:59















Do the squares have to be axis aligned? That makes it much easier
– Ross Millikan
Jul 28 at 5:05




Do the squares have to be axis aligned? That makes it much easier
– Ross Millikan
Jul 28 at 5:05












@RossMillikan Yes!
– Misha Moroshko
Jul 28 at 5:12




@RossMillikan Yes!
– Misha Moroshko
Jul 28 at 5:12












Does the original rectangle always have integer dimensions, and do the squares need to have an integer side length?
– prubin
Jul 29 at 21:59




Does the original rectangle always have integer dimensions, and do the squares need to have an integer side length?
– prubin
Jul 29 at 21:59










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Assume $W ge H$ (otherwise swap them). If you could pack them perfectly the area would say $S=sqrtfrac WHN$. If you use that side you get $m=lfloor frac HS rfloor$ across the height.

Start with $m+3$ (just a guess for safety) across the height, compute

Side that fits in height $frac Hm+3$

Number required in width $n=lceilfrac Nm+3rceil$

Side that fits in width $frac Wn$
Now decrease the number across the height and recompute the sides
It should increase and then start to decrease. Report the maximum.






share|cite|improve this answer





















  • It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
    – Misha Moroshko
    Jul 28 at 5:36










  • I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
    – Ross Millikan
    Jul 28 at 13:57










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "69"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2864990%2fhow-to-calculate-maximal-square-size-when-fitting-n-squares-in-a-given-container%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Assume $W ge H$ (otherwise swap them). If you could pack them perfectly the area would say $S=sqrtfrac WHN$. If you use that side you get $m=lfloor frac HS rfloor$ across the height.

Start with $m+3$ (just a guess for safety) across the height, compute

Side that fits in height $frac Hm+3$

Number required in width $n=lceilfrac Nm+3rceil$

Side that fits in width $frac Wn$
Now decrease the number across the height and recompute the sides
It should increase and then start to decrease. Report the maximum.






share|cite|improve this answer





















  • It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
    – Misha Moroshko
    Jul 28 at 5:36










  • I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
    – Ross Millikan
    Jul 28 at 13:57














up vote
0
down vote













Assume $W ge H$ (otherwise swap them). If you could pack them perfectly the area would say $S=sqrtfrac WHN$. If you use that side you get $m=lfloor frac HS rfloor$ across the height.

Start with $m+3$ (just a guess for safety) across the height, compute

Side that fits in height $frac Hm+3$

Number required in width $n=lceilfrac Nm+3rceil$

Side that fits in width $frac Wn$
Now decrease the number across the height and recompute the sides
It should increase and then start to decrease. Report the maximum.






share|cite|improve this answer





















  • It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
    – Misha Moroshko
    Jul 28 at 5:36










  • I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
    – Ross Millikan
    Jul 28 at 13:57












up vote
0
down vote










up vote
0
down vote









Assume $W ge H$ (otherwise swap them). If you could pack them perfectly the area would say $S=sqrtfrac WHN$. If you use that side you get $m=lfloor frac HS rfloor$ across the height.

Start with $m+3$ (just a guess for safety) across the height, compute

Side that fits in height $frac Hm+3$

Number required in width $n=lceilfrac Nm+3rceil$

Side that fits in width $frac Wn$
Now decrease the number across the height and recompute the sides
It should increase and then start to decrease. Report the maximum.






share|cite|improve this answer













Assume $W ge H$ (otherwise swap them). If you could pack them perfectly the area would say $S=sqrtfrac WHN$. If you use that side you get $m=lfloor frac HS rfloor$ across the height.

Start with $m+3$ (just a guess for safety) across the height, compute

Side that fits in height $frac Hm+3$

Number required in width $n=lceilfrac Nm+3rceil$

Side that fits in width $frac Wn$
Now decrease the number across the height and recompute the sides
It should increase and then start to decrease. Report the maximum.







share|cite|improve this answer













share|cite|improve this answer



share|cite|improve this answer











answered Jul 28 at 5:25









Ross Millikan

275k21185351




275k21185351











  • It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
    – Misha Moroshko
    Jul 28 at 5:36










  • I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
    – Ross Millikan
    Jul 28 at 13:57
















  • It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
    – Misha Moroshko
    Jul 28 at 5:36










  • I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
    – Ross Millikan
    Jul 28 at 13:57















It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
– Misha Moroshko
Jul 28 at 5:36




It this an O(1) solution? How many checks we need to perform in the worst case before we can stop?
– Misha Moroshko
Jul 28 at 5:36












I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
– Ross Millikan
Jul 28 at 13:57




I don't really know. I'm not sure $3$ is enough margin. Probably it is better to start at $frac Hm$ and go both ways until you see a decrease. I haven't figured out a way to bound how many tries you have to make.
– Ross Millikan
Jul 28 at 13:57












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2864990%2fhow-to-calculate-maximal-square-size-when-fitting-n-squares-in-a-given-container%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What is the equation of a 3D cone with generalised tilt?

Color the edges and diagonals of a regular polygon

Relationship between determinant of matrix and determinant of adjoint?