In O(1) get the coordinate i,j of a diagonally ordered matrix Say you have a matrix like this:

[][]int{
    {0, 2, 5, 9, 14},
    {1, 4, 8, 13, 18},
    {3, 7, 12, 17, 21},
    {6, 11, 16, 20, 23},
    {10, 15, 19, 22, 24},
}


As you can see, it is diagonally ordered

Question:
In Order O(1) if I give you a number N, give me the position i,j.



For example, on a regular ordered matrix

[][]int{
  {0,1,2,3},
  {4,5,6,7},
  {8,9,10,11},
  {13,14,15,16}
}


The solution is

j := n % len(m)
i := n / len(m[0])


With that you get i,j.
Ex: N=9 -> 2,1

But how to get them for diagonals?
These are triangular numbers, so:
$i = \lfloor(-0.5 + \sqrt{0.25 + 2 * n}\rfloor - 1\\
triangular = \frac{i * (i + 1)}{2}\\
j = n - triangular - 1$

Minus one comes from indexing from 1.