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 x
th 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.