0
$\begingroup$

Is there a formula to get the row/column based on a number in the following sequence?

$$ \begin{matrix} 0 \\ 1 & 2 \\ 3 & 4 & 5 \\ 6 & 7 & 8 & 9 \\ \end{matrix} $$

For example,

-------------------
|  N  | Row | Col |
-------------------
|  0  |  0  |  0  |
-------------------
|  1  |  1  |  0  |
-------------------
|  2  |  1  |  1  |
-------------------
|  9  |  3  |  3  |
-------------------
| 10  |  4  |  0  |
-------------------
  • 3
    Any positive number $n$ is of the form $\frac{m(m+1)}{2}$ for some natural number $m$ or $\frac{m(m+1)}{2} + k$ for some natural numbers $m,k$, where $k \leq m$ (Both above uniquely). This $m,k$ will determine the row/column you are in. For example, if $n = 9$, then $n = \frac{3(3+1)}{2} + 3$, so $m=3,k=3$. So you have to sandwich your $N$ between two such numbers, and then find the $k$.2017-01-27

1 Answers 1

1

Finishing off on the lead of астон вілла олоф мэллбэрг, but changing the notation a bit: for a value $n$, let $r$ be the row number and $c$ be the column number of its position. Then $\frac {r(r+1)}2$ is the largest triangular number $\le n$. Setting it equal to $n$ and solving yields $$r = \frac {\sqrt{8n+1} - 1}2$$ but since $r$ must be integer, we have

$$ r= \left\lfloor \frac {\sqrt{8n+1} - 1}2\right\rfloor$$ and $$ c = n - \frac {r(r+1)}2$$