In order to provide some context, here's the problem that took me to the question:
I'm attempting to fill a rectangle of known dimensions (w: width, h: height) with squares whose side is s. I also know the total amount of squares that need to be on the screen, let's call it t. Another restriction that this arrange must meet is that the squares will be aligned in a grid (i.e. there could be "empty" cells on the grid, but the squares must be aligned).
So, the first approach to tackle this, would be to imagine a different problem. Let's say that I know the size of the squares and I want to know how many do they fit inside the rectangle. So, horizontally it would be trunc(w/s) and vertically trunc(h/s) resulting in the next equation:
t = trunc(w/s) * trunc(h/s)
So, with this equation in hand, I would love to solve s, since it's my original unknown, and I would have it. However, I have no idea how to do algebra with this truncations.
EDIT 1: Actually, I made a mistake. Maybe the equation isn't solvable for certain values, but if it's OK to leave "blank" cells, then we can safely operate with the following inequation instead of the original equation:
t $\ge$ trunc(w/s) * trunc(h/s)
Assuming $w$, $h$ and $t$ are given positive integers, where $t \le w \cdot h$, and that $s$ must be a positive integer, the following algorithm finds $s$:
The algorithm starts by assigning an approximate value to $s$ which is always larger or equal to the final value. It then calculates the largest grid size that can be made with this value. If the grid size is too small, it decreases $s$ until the grid size is greater than or equal to the needed size $t$.
In the trials I did, the maximum number of times it cycled through the loop was $3$.
Let me know what you think.