[Question]
  [
We start with a blank 1-indexed sequence:
```
_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,...
```
In the nth step, we fill in every a(n) blanks with the integers greater than 1 starting at the first remaining blank, where a(n) is the nth entry in the sequence.
After the first step:
```
2,_,3,_,4,_,5,_,6,_,7,_,8,_,9,_,10,_,11,_,12,_,13,_,...
```
Note that a(1) has to be 2 because the first integer greater than 1 is 2.
In the second step, we fill in every a(2) blanks. It will be apparent that a(2) must be 2.
```
2,2,3,_,4,3,5,_,6,4,7,_,8,5,9,_,10,6,11,_,12,7,13,_,...
```
In the third step, we fill in every a(3) blanks. From the sequence, a(3) = 3.
```
2,2,3,2,4,3,5,_,6,4,7,_,8,5,9,3,10,6,11,_,12,7,13,_,...
```
In the fourth step, we fill in every a(4) blanks. From the sequence, a(4) = 2.
```
2,2,3,2,4,3,5,2,6,4,7,_,8,5,9,3,10,6,11,3,12,7,13,_,...
```
Eventually:
```
2,2,3,2,4,3,5,2,6,4,7,2,8,5,9,3,10,6,11,3,12,7,13,2,...
```
## Task
Given n, return the nth element of the sequence.
The first 10,000,000 terms of the sequence can be found [here](https://drive.google.com/file/d/0B-UzTw21ElCwNWYxdVpjX1NDOTg/view?usp=sharing).
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). Shortest answer in bytes wins. [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) apply.
  
[Answer]
# [Haskell](https://www.haskell.org/), ~~80~~ 67 bytes
```
g~(a:b)|let k!l=k:take(a-1)l++(k+1)!drop(a-1)l=2!g b
m=g m
(!!)$0:m
```
[Try it online!](https://tio.run/##JchBCsIwEADAe1@xAQ8bgiHxGMgTfEER2WIay@7WUHMUvx4PznGe9OYiMkb9IqXFfqR0YCOZUycuSOdoxTlkF615HK/2n3wxFZZJcwWd1ozG2FNIOpS2HTIotesdsB3b3sHDamGO3scQbuMH "Haskell – Try It Online")
Haskell is the perfect language for defining an infinite list in terms of itself.
[Answer]
## C, 123 bytes
```
f(n){int*p=calloc(n,4),i=0,j,k;for(*p=p[1]=2;i<n;++i)for(j=0,k=i/2?0:2-i;j<n;++j)p[j]||k++%p[i]||(p[j]=k/p[i]+2);n=p[n-1];}
```
[Try it online!](https://tio.run/##RY09DoMwDIX3nsJCQkqaIP461UQ9CGVAqagcaIhQN@Ds1GGpF9vvs9@z2dva4xiElyv57zUY20/TbIXXN6nJFNrpEYd5EYxCW3amQmo8KkUyqo4vRkN59SjuVUboTuZkaF23baNSaWiJJxEFM@ZxU5VEz2Y@KzvcD46FT09eSFgvwMW@IKJKYKBEbg3UBULMhLAwGUSSvnSiYRAkJZ5ff/D0J6mLiPbjBw "C (gcc) – Try It Online")
## Walkthrough
```
f(n){int*p=calloc(n,4),
```
Allocate an array of *n* integers to store the first *n* elements of the sequence. This hardcodes `sizeof(int)` as `4`, which is a safe assumption in most cases and certainly one I'm willing to make in the context of code golf. :)
```
i=0,j,k;
```
These are all counters: `i` for the index of the step we're on, `j` to loop through the sequence looking for empty spaces, and `k` to count how many empty spaces have been seen.
```
for(*p=p[1]=2;i<n;++i)
```
Before we start our main loop, we sneak in an initialization of the first two elements of the sequence to `2`. (`p[0]` = `*(p + 0)` = `*p`.) This throws off the count for `k`, though, but...
```
for(j=0,k=i/2?0:2-i;j<n;++j)
```
... we also do a sneaky initialization of `k`, which tests to see if `i` is less than `2` and corrects the starting value of `k` if so. The inner loop also starts here, which iterates over the entire sequence-so-far during each step.
```
p[j]||k++%p[i]||(p[j]=k/p[i]+2);
```
This line could really use some explaining. We can expand this to:
```
if (!(p[j] || ((k++) % p[i]))) {
    p[j] = k / p[i] + 2;
}
```
by short circuiting, and then by De Morgan's laws and the fact that `0` is falsy in C:
```
if (p[j] == 0 && ((k++) % p[i]) == 0) {
    p[j] = k / p[i] + 2;
}
```
This essentially states: "if this space is empty, increment `k`. And if `k` was previously a multiple of the step size, run the following statement." Hence, we run the statement on every *step size* elements, which is exactly how the sequence is described. The statement itself is simple; all it does is generate `2`, `3`, `4`, ....
```
n=p[n-1];}
```
Using the tricky-return-without-a-return that works with `gcc`, we "return" the last element of the first *n* terms in the sequence, which happens to be the *n*th term.
[Answer]
# Pyth, 29 bytes
```
M?tH?eJ.DtHg1GghG-tHhJ+2hJ2g1
```
[Try it online](https://pyth.herokuapp.com/?code=M%3FtH%3FeJ.DtHg1GghG-tHhJ%2B2hJ2g1&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10%0A11%0A12%0A13%0A14%0A15%0A16%0A17%0A18%0A19%0A20)
### How it works
Instead of fooling around with lists, this uses a plain recursive formula.
```
M                                def g(G, H):
 ?tH                                 if H - 1:
      J.DtHg1G                           J = divmod(H - 1, g(1, G))
    ?e                                   if J[-1]:
              ghG-tHhJ                       return g(G + 1, H - 1 - J[0])
                                         else:
                      +2hJ                   return 2 + J[0]
                                     else:
                          2              return 2
                           g1Q   print(g(1, eval(input())))
```
[Answer]
# [Haskell](https://www.haskell.org/), 67 bytes
```
0%j=2
i%j|d<-div i$f j=last$d+2:[(i-d-1)%(j+1)|d*f j<i]
f=(%1).pred
```
[Try it online!](https://tio.run/##NVHLboMwELzzFT4ECZqH2LW9QBW@o4coByRIY0oiBLSHqv9Od1z1gD14d@dh39vlox/HbSvSoeEkpMNPdz524cuE3c0Mzdgu667b8@slC8fuSHmaDXvKf7oXrZ7DNbk1WUr5aZr7bpv75XNcF9OYRzuZm7nQ6XRN1nZ@71c9vPCBD1Y/p6vXXRSVulf6V@sZFXpEBMRaIPSS0zJ57STRJiqBKrRSrWUuMMAEPsYYR2oXh6HBAgoulZkjEdcKbQE6CynLILVWBW2kth5QwG9LNFRQsbX6cAWkHAEyBF2M46DqkMEJtF0JWMGAQy5fwIYnuPYMMz6G9Q6ePMJ5gTFfIo2v4M/XwFLApajoQRheJV6OODgWH7HAuOAiJbqXGunL4pokjzY89eanOTxXszPts9P1O0xvYb1nTZOb/yf7e6TtFw "Haskell – Try It Online")
A recursive arithmetical solution that turned out basically the same method as [Anders Kaseorg's Pyth answer](https://codegolf.stackexchange.com/a/127586/20260).
This code is covered in warts -- ugly parts that look like they could be golfed away, but I didn't see how. 
The function `i%j` really wants to use a guard to check whether `mod i(f j)>0` and evaluate one of corresponding two expression. But, both expressions use `div i(f j)`. Binding that in a guard won't make it apply to both sides. As far as I know, a guard can't be made to "distribute" over other guards. `let` and `where` are too long. So, the code uses `last` to pick one of two expressions while the guard binds the variable. Ugh.
Ideally we'd use `divMod` because both the `div` and `mod` are used, but `(d,m)<-divMod ...` is a long expression. We instead hackily check of the mod is nonzero by seeing if the `div` value times the divisor falls short of the original value.
The `0%j=2` case would not be needed if Haskell short-circuited `div 0`, which it doesn't. The `.pred` converts the 1-indexed input to zero-indexed, or else there would be `-1` corrections everywhere.
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~20~~ 16 bytes
```
!ƒψΣz`:tNΘC←←¹²t
```
[Try it online!](https://tio.run/##yygtzv6vkPuoqdHvv@KxSec7zi2uSrAq8Ts3w/lR2wQgOrTz0KaS//8B "Husk – Try It Online")
A golfed port of the mind-bending [Haskell answer by Anders Kaseorg](https://codegolf.stackexchange.com/a/127458/62393)
### Explanation
This answer builds this meta list with a self-referential process.
Let's get a few things out of the way before we can look at how the list is actually built: `!` simply takes an item from the list, since the challenge asks to return the element at a given list. `ƒ` is the fixpoint operator, it takes a function and applies the function to itself ad infinity, i.e. `ƒg` = `g(g(g(g(g(g(...`; this is what allows us to define the list in terms of the list itself. `ψ` is a way to write a recursive function in Husk: the following commands define a function of one argument where `⁰` (or `¹`) marks the argument, and `²` marks the function itself.
The inner code (with implicit parameters made explicit) is this:
```
Σz`:tNΘC←←⁰²t⁰    Takes an infinite list as input, returns an infinite list
           ²      Apply the function recursively
            t⁰      to the tail of the list
         ←⁰       then take the head of the input list
        ←          -1
       C          and cut the resulting list in groups of that length
      Θ           Prepend an empty group
 z           For each group
  `:          append
    tN        a natural number (starting from 2)
Σ            Then concatenate all groups together
```
What this function does is taking the current `a(n)` and the list of values that are marked as blanks in step `n`, and add `2,3,4,5...` at the right locations between those "blanks".
It would be shorter (and more natural) to prepend a number to each group (each value 2,3,4... goes at the *beginning* of the group of a(n) blanks, after all), but to do that we wouldn't be able to generate the first element of the list until we generate the first group of blanks, and since this definition is self-referential we couldn't generate the first element of the first group of blanks until we generate the first group of blanks after that, and so on forever. By adding an empty group at the beginning and putting values at the end of groups instead, we can generate the initial `2` without having to look at the rest of the list, and this is enough to kickstart the whole process that will generate the infinite list recursively.
[Answer]
# JavaScript (ES6), ~~98 93 91~~ 87 bytes
*Saved 4 bytes thanks to @l4m2*
A recursive function that stops as soon as the result is available.
```
f=(n,p,a=[])=>a[n-1]++||f(n,-~p,[...a].map(c=>c?c:i?i++%(a[p]||2)?c:++v:(i=1,v=2),i=0))
```
[Try it online!](https://tio.run/##FY1BDoMgFET3nuJvGiEfiZp0o/16EMKCUG1oWiDauCnt1Sndzby8ydzNYXa7ufhqfLguOa/EvIjCkNKcJqN802nElNaCm28USkpptHyayCxNdraDmx3iiRkVdUo9LwTxGJijThzUc@Go5TyvYWMeCLoRPFwIzm0JiBzeFYANfg@PRT7CjdVG1YBFQqh1GfxLOed8rD75Bw "JavaScript (Node.js) – Try It Online")
[Answer]
# Java 8, 124 bytes
```
(i)->{int j=1,a[]=new int[i+1],k,s,n;for(;a[i]<2;){for(k=0,n=2;a[++k]>0;);for(s=a[j++]|2*k;k<=i;k+=s)a[k]=n++;}return a[i];}
```
Lambda expression. 
Creates an integer array and continually populates it until the nth value gets populated.
Pre-declaring variables at the top to cut down on as many declarations as possible as each `int` costs 4 bytes of space as opposed to adding `,n` which is 2.
On the `j`'th iteration of calculation, the number of 'blanks' one has to skip is equal to `a[j]` (or 2, if blank). It works out that if the first blank space we have to fill in is at position `k`, `k * a[j]` gives us the 'step' (`s`).
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 104 bytes
```
j;k;f(n){int*p=calloc(n,4)-4,i=1,y;for(;j=k=p[n],!j;++i)for(y=p[i]?:2;j++<n;)p[j]||k++%y||(p[j]=k/y+2);}
```
[Try it online!](https://tio.run/##RYxBDoIwEEX3nmIkIenYEgFZOTQeBFmQGkxbLIS4IcDZa8vG2fzMe/lfZW@lvDdkqWcOV@2@l0mqbhhGxZyoMKuEloVYqB9nRkZaOTWuFWdDnGuMcAlEt497SYbz2hFOjWm3zXKeLtvG4iftdeEl0u7DPnw67RjCeoJwYQFYpBokFBSihlueE8R5mOagepakL5EI6JlGpKP2F093mNCJbvc/ "C (gcc) – Try It Online")
Trying to golf [the last C solution](https://codegolf.stackexchange.com/a/127407/) but it's just too hard to understand
```
f(n){
    int*p=calloc(n,4)-4,i=0,j,k,y;         // p as Array[1..n]
    for(;j=k=p[n],!j;++i)                  // Once p[n] is filled we're over
                                           // and that's what we want
                                           // Otherwise j and k are inited to 0
        for(;j++<n;) p[j]||                // For zeros
            k++%(y=p[i]?:2)||(p[j]=k/y+2); // On correct time fill them
}
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 23 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
ü→è«wε⌠╒└I◄►¼═Öà=╬à╞⌐-Ω
```
[Run and debug it](https://staxlang.xyz/#p=811a8aae77eef4d5c0491110accd99853dce85c6a92dea&i=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10%0A11%0A12%0A13%0A14%0A15%0A16%0A17%0A18%0A19%0A20%0A21%0A22%0A23%0A24%0A&m=2)
A direct implementation of the procedure.
Uses zeroes as placeholders till the array is fully filed, and take the last element.
## Explanation
```
z({c:0ni@i1>s2?::{i^^&F}x*H Input: x
z                           empty list
 (                          pad with x zeroes (call this S)
  {                    }x*  perform the following x times:
   c:0                      get the falsy indices
         i1>  ?             if iteration index >1 :
      ni@                   get nth element of S
            2               else push 2
               ::           get every nth element of the falsy indices
                 {    F     for each index:
                  i^^&      replace with iteration index + 2
```
]