I am looking for an optimum way to create a table from n elements so that ideally there are no empty cells, but at the same time the proportion of the table dimensions colums / rows becomes as close to 1 as possible.
Of course if n is a square number it is easy since then
cols = rows = sqrt( n );
If n is a prime number it is also clear that there will be empty cells, so my current way to handle this is:
rows = floor( sqrt(n) );
cols = ceil( n / rows );
For all other cases my plan is to get the prime factors of n and then search all possible permutations for those who's combination has proportions closest to 1.
So my question is: is there is a better way to do this? Or is there at least a way not having to test every possible combination of the prime factors?