| Bytes | Lang | Time | Link |
|---|---|---|---|
| 063 | R | 251013T094626Z | Glory2Uk |
| 016 | Husk | 210317T061025Z | Leo |
| 023 | Stax | 210317T091715Z | Razetime |
| 087 | JavaScript ES6 | 170620T101845Z | Arnauld |
| 104 | C gcc | 210317T081552Z | l4m2 |
| 067 | Haskell | 170620T230804Z | xnor |
| 029 | Pyth | 170620T204449Z | Anders K |
| 067 | Haskell | 170620T114140Z | Anders K |
| 124 | Java 8 | 170620T182109Z | Xanderha |
| 123 | C | 170620T083123Z | Doorknob |
R, 63 bytes
`>`=\(a=1,b)`if`(u<-b-1,`if`(u%%(r=1>a),a+1>u-u%/%r,2+u%/%r),2)
I have followed the published recursive solution, explained in this post of Anders Kaseorg.
Husk, 20 16 bytes
!ƒψΣz`:tNΘC←←¹²t
A golfed port of the mind-bending Haskell answer by Anders Kaseorg
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.
Stax, 23 bytes
ü→è«wε⌠╒└I◄►¼═Öà=╬à╞⌐-Ω
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
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))
C (gcc), 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);}
Trying to golf the last C solution 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
}
Haskell, 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
A recursive arithmetical solution that turned out basically the same method as Anders Kaseorg's Pyth answer.
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.
Pyth, 29 bytes
M?tH?eJ.DtHg1GghG-tHhJ+2hJ2g1
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())))
Haskell, 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
Haskell is the perfect language for defining an infinite list in terms of itself.
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).
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];}
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 nth term.