Function generating $n$ copies of $n$

66 Views Asked by At

I need a function that, starting at $0$ or $1$, gives the sequence $$1,2,2,3,3,3,4,4,4,4,5,5,5,5,5...$$ In other words, has $n$ copies of $n$. What is the simplest such function, hopefully non-piecewise?

3

There are 3 best solutions below

1
On

$$f(n) = \left\lfloor \frac{1}{2} + \sqrt{2n} \right\rfloor$$

Or, if you let $[x]$ be the nearest integer to $x$ it simplifies to $f(n) = [\sqrt{2n}]$.

0
On

The end of the run of $n$ is $T_n$, the $n^{th}$ triangular number. $T_n=\frac 12n(n+1)$ You want to invert this relationship. Given $k$, the position in the list, you want to find the $n$ that has $T_{n-1} \lt k \le T_n$. That is $n=\lceil \frac 12(1+\sqrt{8k+1})\rceil$

0
On

Meanwhile, the reason that the rounding will never be ambiguous is that, if we ever had (positive integer) $n$ and integer $w$ with $$ \sqrt {2n} = w + \frac{1}{2} \; , $$ we would then have $$ 2n = w^2 + w + \frac{1}{4} $$ which is impossible because $1/4$ is not an integer