I am looking for a way to determine the X & Y position of a number to draw following square:
1 2 5 10 17
3 4 6 11
7 8 9 12
13 14 15 16
What kind of algorithm / formula can I use ?
I have tried rounding the square root of the number to determine the X position.
To be clear, here is what I want to achieve, numbers with their corresponding X and Y position:
# X Y
1: 1 1
2: 2 1
3: 1 2
4: 2 2
5: 3 1
6: 3 2

Note the pattern in your table:
The algorithm of filling the table is as follows:
start with a 1×1 square with a single
1in it:add a column along the right side of the defined area, filling it with consecutive natural numbers (it will be 1-item column with a single
2in it):add a row along the bottom side of the defined area, filling it with consecutive natural numbers (it will be 2-items row with numbers
3&4):add a column along the right side of the defined area, filling it with consecutive natural numbers (it will be 2-item column with numbers
5&6):add a row along the bottom side of the defined area, filling it with consecutive natural numbers (it will be 3-items row with numbers
7through9):and so on, expand the area by a column and by a row, a column and a row, ...
So, after completing a $k\times k$ square you make a new one of size $(k+1)\times (k+1)$, by placing numbers $k^2+1\dots k^2+k$ in a column $(k+1)$, rows $1$ through $k$, and then numbers $k^2+k+1\dots k^2+2k+1 = (k+1)^2$ in row $(k+1)$, columns $1 \dots k+1$.
Recovering coordinates for a given $n$ is quite easy now: find the largest square $k^2$ less than your number: $$k^2\lt n$$ and test if $n$ is in the $(k+1)$-st column or $(k+1)$-st row.
If $k^2 \lt n \le k^2+k$, then $row=n-k^2, col=k+1$.
If $k^2+k \lt n \le (k+1)^2$, then $row=k+1, col=n-(k^2+k)$.