The Jumping Up Sequence Consider the following sequence:

0 1 3 2 5 4 8 6 7 12 9 10 11 17 13 14 15 16 23 ...


Looks pretty pattern-less, right? Here's how it works. Starting with 0, jump up n integers, with n starting at 1. That's the next number in the sequence. Then, append any numbers "skipped" and that haven't been seen yet in ascending order. Then, increment n and jump from the last number appended. Repeat this pattern.

So, for example, when we reach 11, we're at n=5. We increment n to be n=6, jump up to 17, then append 13 14 15 16 since those haven't been seen yet. Our next jump is n=7, so the next element in the sequence is 23.

The Challenge

Given input x, output the xth term of this sequence, the first x terms of the sequence, or build an infinite list of the terms of the sequence. You can choose 0- or 1-indexing.

I/O and Rules


The input and output can be given by any convenient method.
The input and output can be assumed to fit in your language's native number type.
Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
Standard loopholes are forbidden.
This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
JavaScript (ES7), 41 bytes

Returns the n-th term of the sequence, 0-indexed.

n=&gt;(d=(n--*8-23)**.5)%1?n:'121'[n]^n-~d/2


Try it online!

How?

Main case: \$n &gt; 3\$

The first four terms of the sequence are special, so let's put them aside for now.

For \$n &gt; 3\$, the sequence looks like that:

 n  | [4] 5 [6] 7 8 [ 9] 10 11 12 [13] 14 15 16 17 [18] 19 20 21 22 23 [24] 25 26 27 ...
----+------------------------------------------------------------------------------------
a(n)| [5] 4 [8] 6 7 [12]  9 10 11 [17] 13 14 15 16 [23] 18 19 20 21 22 [30] 24 25 26 ...
----+------------------------------------------------------------------------------------
 k  |  2  -  3  - -   4   -  -  -   5   -  -  -  -   6   -  -  -  -  -   7   -  -  - ...


We can notice that there are in fact two interleaved sub-sequences:


Most values belong to the sub-sequence \$A\$ for which we simply have:

$$A(n) = n - 1$$
Some other values belong to the sub-sequence \$B\$ (highlighted with brackets in the above diagram) whose indices follow the arithmetic sequence 3, 3, 4, 6, 9, 13, 18, 24 ... and for which we have:

$$B(n,k) = n + k - 1$$

where \$n\$ is the index in the main sequence and \$k\$ is the index in the sub-sequence \$B\$.


The indices of \$B\$ in the main sequence are given by:

$$n_k = \frac{k^2 - k + 6}{2}$$

Given \$n\$, we know that the corresponding term in the main sequence belongs to \$B\$ if \$n\$ is an integer solution of the quadratic equation:

$$x^2 - x + 6 - 2n = 0$$

whose discriminant is:

$$\Delta = 1 - 4(6 - 2n) = 8n - 23$$

and whose positive solution is:

$$x = \frac{1 + \sqrt{\Delta}}{2}$$

We expect \$\sqrt{\Delta}\$ to be an integer. Hence the test:

(d = (n-- * 8 - 23) ** .5) % 1


Special cases: \$0 \le n \le 3\$

For \$n &lt; 3\$, the discriminant is negative and taking its square root results in NaN. For \$n = 3\$, the discriminant is \$1\$. Therefore, these first four terms of the sequence are considered to belong to the sub-sequence \$B\$.

Should we just apply our standard formula n - ~d / 2, we'd get:

$$-\frac 1 2, \frac 1 2, \frac 3 2, 3$$

instead of:

$$0, 1, 3, 2$$

Which is why we XOR these results with \$0,1,2\text{ and }1\$ respectively.