I have a set of numbers which I need to put on a rectangular grid as dense as possible (=the area of a grid is minimal). For example if I have to put numbers 16, 31, and 63, I can put them into 2x2-grid
1 6
3 0.
I think the best way to solve the problem for an arbitrary collection of numbers is to make some kind of net or tree which tries every permutation of numbers and the directions they can be read. I have difficulties to find a proper NextPosition()-algorithm which adds a number to a grid and tries every possible order of given numbers, positions where a given number starts, and direction of a given number. Can anyone suggest a method which tries every possible position?