25

There exists a formula for the $n$th term of this sequence A002024 from the OEIS ("$n$ appears $n$ times") $$1, 2, 2, 3, 3, 3, 4, 4 ,4, 4, 5...$$ which is $$\left \lfloor \frac {1+\sqrt{1+8n}}{2} \right \rfloor.$$

Is there a better formula for the $n$th term? A formula that isn't a 'magic' one that just happens to work but one that comes from a well-founded observation. Preferably one not using the floor function. I also have suspicions about the sustainability of the above formula as I feel with very $n$ it may not function properly.

user642796
  • 52,188
Ali Caglayan
  • 5,726

4 Answers4

25

I don't think there is a better formula, as it is based on two basic observations:

  • $\displaystyle \sum_{k=0}^{m-1}k=\frac{m(m-1)}{2}$.

  • $\displaystyle \frac{m(m-1)}{2}=n$ with $m>0$ $\Longrightarrow$ $\displaystyle m=\frac{1+\sqrt{1+8n}}{2}$.


Further explanations:

We have a sequence $\{a_n\}$ such that $a_0=1$, $a_1=a_2=2$, $a_3=a_4=a_5=3$, etc, and we want to have a general formula for $a_n$.

To obtain it, let us first ask a somewhat inverse question: for which $n$'s $a_n$ will be equal to a given number $m$? Here we will use the first formula: the numbers from $1$ to $m-1$ occupy $1+2+3+\ldots+(m-1)=m(m-1)/2$ first positions in the sequence. Therefore the number $m$ will correspond to $m$ values of $n$ explicitly given by $$n=\frac{m(m-1)}{2}+1,\frac{m(m-1)}{2}+2,\ldots,\frac{m(m-1)}{2}+m\tag{1}$$ Now we come back to our initial question. Given $n$, (1) implies that $a_n$ will be equal to the largest integer $m$ such that $\displaystyle \frac{m(m-1)}{2}\leq n$. This largest integer is obtained by solving the equation $\displaystyle\frac{m(m-1)}{2}= n$ for $m$ (here we use the 2nd formula) and taking the integer part of the solution.

Start wearing purple
  • 53,234
  • 13
  • 164
  • 223
17

There is a better formula: $$a_n = \operatorname{round}{\sqrt {2n}} $$

Where $\operatorname{round}$ means to round to the nearest integer, or if you prefer, $\operatorname{round}{x} = \left\lfloor x+\frac12\right\rfloor$

MJD
  • 65,394
  • 39
  • 298
  • 580
3

You got nice answers already, so I'll just address the "sustainability" issue.

The formula you're discussing uses a real (non-integer) arithmetic to compute the integer solution of an integer problem. Theoretically, it is possible that at some point the square root is computed with a minor error and that the obtained solution is wrong (it may depend on processor, as well as some other factors).

To be absolutely certain that your program gives a correct solution (of course, as long as $n$ in the range of integers), you'll have to stick to the integer arithmetic. However, this means computing (but not remembering) the elements of your sequence, up to the $n$-th one. I don't think there is an integer-arithmetic formula that would save you from this.

A drawback? The speed, obviously, as such algorithm performs in $O(n)$ time, while the formula above computes in the constant time. This is significant, since the error should not occur for any reasonably small $n$.

All in all, I'd go with the above formula, as I'm fairly certain that this would not happen, since the square root would not allow lose $1$ as the significant digit, and I see no other threats, assuming that the processor is not buggy (i.e., it doesn't compute $\sqrt{x}$ to have a smaller integer part than the actual $\sqrt{x}$).

If it's absolutely crucial to get correct results for really large $n$, you might want to do some error estimate and see if the formula can actually produce wrong results. Again, I'm fairly certain that it can not.

Vedran Šego
  • 11,372
  • Thank you for your contribution on addressing the accuracy situation. The reason that I have suspicions with it was because of the floor function as it implies that without it the answer is not accurate. And if it is relying on rounding down then the decimal fraction could have a possibility of skipping integer bounds. – Ali Caglayan Jul 30 '13 at 17:55
  • Yes, that is a general problem. However, your formula is very specific. So, when can the error occur? The answer is: when $1+\sqrt{1+8n}$ is computed as $<2x$ for some $x \in \mathbb{N}$, while in reality it should be $\ge 2x$. One obvious reason is that $\sqrt{8n}$ is computed as $< 2x-1$ instead of $\ge 2x$, but I don't think a square root gets you this kind of an error on modern computers (I wouldn't even expect the old ones to miss that much). – Vedran Šego Jul 30 '13 at 18:46
  • 1
    You can calculate the integer square root of $1+8n$, that is $\left\lfloor\sqrt{1+8n}\right\rfloor$, using only integer operations, in $O(\sqrt n)$ time, and I think probably in $O(\log n)$ time. – MJD Jul 30 '13 at 18:46
  • The less obvious reason would be for $1+\sqrt{1+8n}$ to be computed the same value as $\sqrt{1+8n}$, i.e., that this expression loses the least significant integer digit. However, in 64-bit arithmetic, $8n+1$ would have 64 bits, i.e., around $3 \cdot 6 + 1 = 19$ digits. Taking a square root, you are left with approximately $10$ integer digits, while the double arithmetic has around $16$ significant digits, so this case should also present no problems. – Vedran Šego Jul 30 '13 at 18:49
  • Of course, you can listen to MJD's idea, sticking only to the integer arithmetic to keep it safer, but - as I explained - I see no place where the ordinary double precision arithmetic would fail in this case. The emphasis being on this case, because - generally - computing integer stuff with the real arithmetic can cause trouble. – Vedran Šego Jul 30 '13 at 18:51
  • @MJD $O(\log(n)^3)$ using a simple binary search and multiplication since $\sqrt{1 + 8n}$ is monotonic. – DanielV Sep 29 '14 at 21:56
  • @MJD definitely $O(\log{ n})$ time, using the old-fashioned square root extraction procedure. It's even better if you have a processor intrinsic to get the highest bit of a number. – Spencer Aug 25 '20 at 17:26
1

Let $a(n)$ denote the $n$th element of the sequence. According to the OEIS,

$$\begin{cases} a(1) &= 1\\ a(n) &= a(n - a(n - 1)) + 1. \end{cases}$$

Proof of this formula. Two easy observations:

(1) If $\Delta$ is a triangular number, then $\Delta - a(\Delta)$ is the previous triangular number.

(2) If $\Delta'$ is a triangular number, $\Delta$ the previous triangular number and $n$ an integer such that $\Delta < n \leq \Delta'$, then $$a(n) = a(\Delta') = a(\Delta + 1) = a(\Delta) + 1.$$

Let $n \geq 2$. Let $\Delta$ be the greatest triangular number such that $\Delta < n$ and $\Delta'$ the least triangular number such that $n \leq \Delta'$.

First case. $\Delta = n - 1$. Then $$a(n) = a(\Delta + 1) \overset{(2)}{=} a(\Delta) + 1 \overset{(1),\,(2)}{=} a(\Delta - a(\Delta) + 1) + 1 = a(n - a(n-1)) + 1.$$

Second case. $\Delta \neq n - 1$. Then

(3) $\Delta < n-1 \leq \Delta'$ (follows from the def. of $\Delta$, $\Delta'$),

(4) $a(n) = a(n-1)$ (due to (2) and (3)).

Hence $$\Delta - a(\Delta) \overset{(2)}{=} \Delta - (a(n) - 1) \overset{(3)}{<} n - 1 - (a(n) - 1) = n - a(n)$$ and $$n - a(n) \overset{(2)}{=} n - a(\Delta') \leq \Delta' - a(\Delta') \overset{(1)}{=} \Delta$$ which combined give $$\Delta - a(\Delta) < n - a(n) \leq \Delta.$$ This last line finally implies $$a(n) \overset{(2)}{=} a(\Delta) + 1 \overset{(2)}{=} a(n - a(n)) + 1 \overset{(4)}{=} a(n - a(n - 1)) + 1.$$

Silenus
  • 53