Find the pyramid row by index?

Given a pyramid like:

      0
     1 2
    3 4 5
   6 7 8 9
     ...

and given the index of the pyramid i, where irepresents the ieth number of the pyramid, is there any way to find the index of the row to which the ith element belongs ? (for example, if i = 6,7,8,9, it is in the third line, starting from line 0)

+4
source share
3 answers

. n- , T n, T n= n ( -1)/2. 0, 1, 3, 6, 10, 15 .., , n- ( , , .)

, - n , T n & le; . - O (& radic; n), T 0, T 1, T 2 .., , . , O (log n), T 1, T 2, T 4, T 8 .., , .

. , n,

n (n + 1)/2 = i

,

n 2/2 + n/2 = i.

,

n 2/2 + n/2 - = 0,

, :

n 2 + n - 2i = 0.

:

n = (-1 pm; & radic; (1 + 8i))/2

, , , n

n = (-1 + & radic; (1 + 8i))/2.

, , , :

row = & llolo; (- 1 + & radic; (1 + 8i))/2 & rfloor;.

:

int row = int((-1 + sqrt(1 + 8 * i)) / 2);

, , . 9 ? ,

(- 1 + & radic; (1 + 72))/2 = (-1 + & radic; 73)/2 = 3,77

, , 3 - !

, 55? ,

(1 + 440))/2 = (& radic; 441 - 1)/2 = 10

10. - T 10= 55, 55 . , !

+5

, i th n - , n - n(n+1)/2 <= i < (n+1)(n+2)/2

, i = 6, n = 3, n(n+1)/2 <= 6 i = 8, n = 3, n(n+1)/2 <= 8

0

row = math.floor(√ (2i + 0.25) - 0.5) -

, , n 2 + n (n + 0,5) 2 - 0.25

0

All Articles