6 saved 2 bytes
source | link

JavaScript (ES6), 92 82 69 6767 65 bytes

n=>(a={},F=i=>i>nF=i=>i^n?b:F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1):2*b]=i+1]=-~i):b)(1a={})

How?

We keep track of:

  • theThe last inserted value b.
  • allAll previously encountered values in the lookup table a.

At even positionsInternally, the next value is simplywe're using a 0-based index 2 * bi. Odd and even behaviors are therefore inverted:

  • At odd positions, the next value is simply 2 * b.

  • At even positions, we use the recursive function g() and the lookup table a to identify the smallest matching value:

    (g = k => a[k] ? g(k + 1) : k)(1)
    

At odd positionsTo save a few bytes, we use the recursive function g()i and the lookup tableis initialized to a{} rather than 0. This compels us to identify the smallest matching valueuse:

(g = k => a[k] ? g(k + 1) : k)(1)
  • i^n to compare i with n because ({}) ^ n === n whereas ({}) - n evaluates to NaN.
  • -~i to increment i, because ({}) + 1 would generate a string.

Demo

let f =

n=>(a={},F=i=>i>nF=i=>i^n?b:F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1):2*b]=i+1]=-~i):b)(1a={})

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

JavaScript (ES6), 92 82 69 67 bytes

n=>(a={},F=i=>i>n?b:F(a[b=i&1?(g=k=>a[k]?g(k+1):k)(1):2*b]=i+1))(1)

How?

We keep track of:

  • the last inserted value b
  • all previously encountered values in the lookup table a

At even positions, the next value is simply 2 * b.

At odd positions, we use the recursive function g() and the lookup table a to identify the smallest matching value:

(g = k => a[k] ? g(k + 1) : k)(1)

Demo

let f =

n=>(a={},F=i=>i>n?b:F(a[b=i&1?(g=k=>a[k]?g(k+1):k)(1):2*b]=i+1))(1)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

JavaScript (ES6), 92 82 69 67 65 bytes

n=>(F=i=>i^n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=-~i):b)(a={})

How?

We keep track of:

  • The last inserted value b.
  • All previously encountered values in the lookup table a.

Internally, we're using a 0-based index i. Odd and even behaviors are therefore inverted:

  • At odd positions, the next value is simply 2 * b.

  • At even positions, we use the recursive function g() and the lookup table a to identify the smallest matching value:

    (g = k => a[k] ? g(k + 1) : k)(1)
    

To save a few bytes, i is initialized to {} rather than 0. This compels us to use:

  • i^n to compare i with n because ({}) ^ n === n whereas ({}) - n evaluates to NaN.
  • -~i to increment i, because ({}) + 1 would generate a string.

Demo

let f =

n=>(F=i=>i^n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=-~i):b)(a={})

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

5 added the 'How?' section
source | link

JavaScript (ES6), 92 82 69 67 bytes

n=>(a={},F=i=>i<nF=i=>i>n?b:F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=i+1:2*b]=i+1))(1)

How?

We keep track of:

  • the last inserted value b
  • all previously encountered values in the lookup table a

At even positions, the next value is simply 2 * b.

At odd positions, we use the recursive function g() and the lookup table a to identify the smallest matching value:

(g = k => a[k] ? g(k + 1) :b k)(01)

Demo

let f =

n=>(a={},F=i=>i<nF=i=>i>n?b:F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=i+1):b2*b]=i+1))(01)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

JavaScript (ES6), 92 82 69 67 bytes

n=>(a={},F=i=>i<n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=i+1):b)(0)

Demo

let f =

n=>(a={},F=i=>i<n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=i+1):b)(0)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

JavaScript (ES6), 92 82 69 67 bytes

n=>(a={},F=i=>i>n?b:F(a[b=i&1?(g=k=>a[k]?g(k+1):k)(1):2*b]=i+1))(1)

How?

We keep track of:

  • the last inserted value b
  • all previously encountered values in the lookup table a

At even positions, the next value is simply 2 * b.

At odd positions, we use the recursive function g() and the lookup table a to identify the smallest matching value:

(g = k => a[k] ? g(k + 1) : k)(1)

Demo

let f =

n=>(a={},F=i=>i>n?b:F(a[b=i&1?(g=k=>a[k]?g(k+1):k)(1):2*b]=i+1))(1)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

4 saved 2 bytes
source | link

JavaScript (ES6), 92 82 6969 67 bytes

n=>(a={},F=i=>i<n?F(i+1,a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=1]=i+1):b)(0)

Demo

let f =

n=>(a={},F=i=>i<n?F(i+1,a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=1]=i+1):b)(0)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

JavaScript (ES6), 92 82 69 bytes

n=>(a={},F=i=>i<n?F(i+1,a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=1):b)(0)

Demo

let f =

n=>(a={},F=i=>i<n?F(i+1,a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=1):b)(0)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

JavaScript (ES6), 92 82 69 67 bytes

n=>(a={},F=i=>i<n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=i+1):b)(0)

Demo

let f =

n=>(a={},F=i=>i<n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=i+1):b)(0)

for(n = 1; n <= 20; n++) {
  console.log('a[' + n + '] = ' + f(n));
}

3 saved 13 bytes
source | link
2 saved 10 bytes
source | link
1
source | link