java - Derive number of number pyramid from index number -


the problem goes this:

suppose have number n, value used create number pyramid. number pyramid n= 4 this:

      3     2 3   1 2 3 0 1 2 3 

equivalently, this:

      0     1 1   2 2 2 3 3 3 3 

needless say, want know of way traverse through every number of pyramid using index. seeking function akin to:

f(i) = [number pyramid]

where i index number. best if dependent on index (i.e. not recursive).

i've tried pattern in indexed list like

n=4 ([0 0] [1 1] [2 1] [3 2] [4 2] [5 2] [6 3] [7 3] [8 3] [9 3])

the first number in each pair index, second number pyramid.

alas, i've had no luck in finding clear pattern.

an elaboration on egor's answer:

the first occurrence of x at

sum(0<=i<=x | i) = x(x+1)/2 

now assume have index i, first solve if function not discrete , round down in end:

x(x+1)/2 =      <=> x^2 + x - 2i = 0 

solve quadratic equation:

x = (-1 +/- sqrt(1 + 8i)) / 2 

simplifying, ignoring negative solution , rounding down yields formula given egor:

f(i) = floor((sqrt(8i+1)-1)/2) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -