Explore a Klarner-Rado sequence
05AB1E, 15 14 bytes
$FDx>DŠ+)˜ê}sè
Try it online!
JavaScript (ES6), 53 50 bytes
1-indexed.
n=>(g=k=>n?g(g[k]|!k++?g[n--,k*2]=g[k*3]=k:k):k)``
Try it online!
Commented
Note: On the first iteration, we have k = ['']
, which is zero-ish but truthy. By doing !k++
, we force k
to be coerced to \$0\$ right away (and not just before it's incremented), which makes the test work as expected.
n => ( // n = requested index
g = k => // h is a recursive function taking k, starting at 0
n ? // if n is not equal to 0:
g( // do a recursive call:
g[k] | // if g[k] is defined
!k++ ? // or k = 0 (increment k after the test):
g[n--, k * 2] = // decrement n; set g[k * 2]
g[k * 3] = k // and g[k * 3] and pass k
: // else:
k // just pass k
) // end of recursive call
: // else:
k // stop recursion and return k
)`` // initial call to g with k = [''] (zero-ish)
JavaScript (ES6), 65 bytes
I thought the '11'[k/x-2]
trick was neat, but overall this initial approach is far too long.
0-indexed.
n=>(g=k=>a[n]||g(-~k,a.some(x=>'11'[k/x-2])&&a.push(k+1)))(a=[1])
Try it online!
Commented
n => ( // n = requested index
g = k => // g is a recursive function taking k (starting at 1)
a[n] || // if a[n] is defined, return it and stop
g( // otherwise, do a recursive call:
-~k, // with k + 1
a.some(x => // if there exists some x in a[]
'11'[k / x - 2] // such that k / x is either 2 or 3 ...
) //
&& a.push(k + 1) // ... then push k + 1 in a[]
) // end of recursive call
)(a = [1]) // initial call to g with k = a = [1]