[Math] Fitting n number of squares into n area

geometry

I am a mobile developer and I have a problem and need to find a formula to get the dimensions of squares to fit inside a space. My problem is that I have a rectangle of dimensions lxw and need to fit n number of squares into it. Where n can change to any number from 2 up to 30 or more. While l and w can be any dimension but will mainly be in the range upward of 600×400. What I need is to fit exactly n number of squares into this space, the dimensions of the squares can go as small or as large as required.

Also I require the the number of rows and columns that will fit these squares in them. The squares will be displayed as evenly as possible across these rows and columns.

I have found a similar solution but I simply don't have the mathematics undestanding to get my head around the explanation. Also the explanation mentions that the grid is 'w squares wide, h squares high' however I don't know how many squares high and wide my screen will be, maybe I'm misunderstanding something? If someone could dumb this formula down for me it would be appreciated.

How can I calculate the size of a square block and the number of rows and columns needed to fit a known number of blocks on a page of known size?

Best Answer

I ended up finding another question which basically answered my question. It has alot of maths behind it which I don't understand but he put a C code example so I was able to follow that and understand what I needed to do. Link is posted below

Algorithm to get the maximum size of n squares that fit into a rectangle with a given width and height