I have a rectangle width x height, and N squares of same unknown size. I must determine the maximum size of these squares and number of rows and columns to fit perfectly (UP
This problem came up recently in a project I was working on. Here is the solution that was determined:
int numItems; // the number of squares we need to pack in.
double rectWidth; // the width of the space into which we want to pack our squares.
double rectHeight; // the height of the space into which we want to pack our squares.
double tableRatio = rectWidth / rectHeight;
double columns = sqrt(numItems * tableRatio);
double rows = columns / tableRatio;
columns = ceil(columns); // the number of columns of squares we will have
rows = ceil(rows); // the number of rows of squares we will have
double squareSize = rectWidth / columns; // the size of each square.
Your question is not consistent. First, you state the problem as "determine the maximum size of these squares and number of rows and columns to fit perfectly into the rectangle." (emphasis added).
But then you give a sample final result that allows empty space.
So which is it?
If you need the squares to fit perfectly in the rectangle with no empty space and no squares extending beyond the bounds of the rectangle, then the size of the maximal square would equal the greatest common divisor of the length and width of the rectangle.
See http://en.wikipedia.org/wiki/Greatest_common_divisor#A_geometric_view