Given this triangular:
$$1$$
$$2,3$$
$$4,5,6$$
$$7,8,9,10$$
$$.............$$
Now, how can i calculate the height of number of some number in this triangular? (Namely, how can i find formula for the height of $n$?)
Given this triangular:
$$1$$
$$2,3$$
$$4,5,6$$
$$7,8,9,10$$
$$.............$$
Now, how can i calculate the height of number of some number in this triangular? (Namely, how can i find formula for the height of $n$?)
For a given value $n$ there exist $h$ such that \begin{eqnarray*} \frac{h(h-1)}{2} < n \leq \frac{h(h+1)}{2}. \end{eqnarray*} Now treat the the right hand in equality as a quadratic in $h$ and solve to get \begin{eqnarray*} h = \left\lceil \frac{-1 + \sqrt{1+8n}}{2} \right\rceil. \end{eqnarray*}
The first entry in the $n$-th row is $\frac{n(n-1)}{2}+1$ and the last is $\frac{n(n+1)}{2}$ so in order to find the row of the number $x$ you have to find $n$ such that
$$\frac{n(n-1)}{2}+1 \leq x \leq \frac{n(n+1)}{2}$$
Pointers:
There are $n$ numbers in row $n$.
The last number of the $n^{\text{th}}$ row is $\dfrac {n(n+1)}{2}$.
The first number in the $n^{\text{th}}$ row is $\dfrac{n(n-1)}{2}+1$.
To calculate the row in which any number $x$ lies, just check the triangular number nearest to it and bigger than it, and it lies in the row number $n$ corresponding to that triangular number.
To find the row that $n$ is on, double $n$, take the square root, and find the nearest integer.
Define a function $h(m)$ recursively:
$h(1) = 1$
Let $h(m)$ be defined.
If $m + 1 \le \dfrac {h(m)(h(m)+1)}{2}$ then $h(m+1) = h(m)$
else
$\qquad \qquad \qquad \qquad \qquad \qquad \qquad \, h(m+1) = h(m) + 1$
This doesn't provide an explicit formula, but I'm pretty sure that a few lines of code in a (suitable) programming language will do the trick. So that is reassuring.
OK, here is some code. I got some help from python experts; see this stackoverflow Q/A link. Note that this code also gives the row position of each number:
def daHB(m):
if m == 1:
return {'rowNum':1, 'posNum':1, 'TriNum':1}
else:
r = daHB(m-1)
if m <= r['TriNum']:
return {'rowNum':r['rowNum'], 'posNum':r['posNum'] + 1, 'TriNum':r['TriNum']}
else:
TriangularNum = (r['rowNum']+1)*(r['rowNum']+2) // 2
return {'rowNum':r['rowNum'] + 1,'posNum':1, 'TriNum':TriangularNum}
for n in range(1, 33):
print (n, daHB(n))
OUTPUT
1 {'rowNum': 1, 'posNum': 1, 'TriNum': 1}
2 {'rowNum': 2, 'posNum': 1, 'TriNum': 3}
3 {'rowNum': 2, 'posNum': 2, 'TriNum': 3}
4 {'rowNum': 3, 'posNum': 1, 'TriNum': 6}
5 {'rowNum': 3, 'posNum': 2, 'TriNum': 6}
6 {'rowNum': 3, 'posNum': 3, 'TriNum': 6}
7 {'rowNum': 4, 'posNum': 1, 'TriNum': 10}
8 {'rowNum': 4, 'posNum': 2, 'TriNum': 10}
9 {'rowNum': 4, 'posNum': 3, 'TriNum': 10}
10 {'rowNum': 4, 'posNum': 4, 'TriNum': 10}
11 {'rowNum': 5, 'posNum': 1, 'TriNum': 15}
12 {'rowNum': 5, 'posNum': 2, 'TriNum': 15}
13 {'rowNum': 5, 'posNum': 3, 'TriNum': 15}
14 {'rowNum': 5, 'posNum': 4, 'TriNum': 15}
15 {'rowNum': 5, 'posNum': 5, 'TriNum': 15}
16 {'rowNum': 6, 'posNum': 1, 'TriNum': 21}
17 {'rowNum': 6, 'posNum': 2, 'TriNum': 21}
18 {'rowNum': 6, 'posNum': 3, 'TriNum': 21}
19 {'rowNum': 6, 'posNum': 4, 'TriNum': 21}
20 {'rowNum': 6, 'posNum': 5, 'TriNum': 21}
21 {'rowNum': 6, 'posNum': 6, 'TriNum': 21}
22 {'rowNum': 7, 'posNum': 1, 'TriNum': 28}
23 {'rowNum': 7, 'posNum': 2, 'TriNum': 28}
24 {'rowNum': 7, 'posNum': 3, 'TriNum': 28}
25 {'rowNum': 7, 'posNum': 4, 'TriNum': 28}
26 {'rowNum': 7, 'posNum': 5, 'TriNum': 28}
27 {'rowNum': 7, 'posNum': 6, 'TriNum': 28}
28 {'rowNum': 7, 'posNum': 7, 'TriNum': 28}
29 {'rowNum': 8, 'posNum': 1, 'TriNum': 36}
30 {'rowNum': 8, 'posNum': 2, 'TriNum': 36}
31 {'rowNum': 8, 'posNum': 3, 'TriNum': 36}
32 {'rowNum': 8, 'posNum': 4, 'TriNum': 36}