OFFSET
0,3
COMMENTS
This sequence is a permutation of the nonnegative integers that preserves the binary length as well as the number of runs. See A330091 for the inverse.
LINKS
FORMULA
If n has w binary runs, then a^A003558(w-1)(n) = n (where a^k denotes the k-th iterate of the sequence).
EXAMPLE
For n = 19999:
- the binary representation of 19999 is "100111000011111",
- the corresponding run lengths are (1, 2, 3, 4, 5),
- hence the run lengths of a(n) are (1, 3, 5, 4, 2),
- and its binary representation is "100011111000011",
- so a(n) = 18371.
PROG
(PARI) torl(n) = { my (rr=[]); while (n, my (r=valuation(n+(n%2), 2)); rr = concat(r, rr); n\=2^r); rr }
shuffle(v) = { my (w=vector(#v), o=0, e=#v+1); for (k=1, #v, w[if (k%2, o++, e--)]=v[k]); w }
fromrl(rr) = { my (v=0); for (k=1, #rr, v = (v+(k%2))*2^rr[k]-(k%2)); v }
a(n) = fromrl(shuffle(torl(n)))
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Rémy Sigrist, Dec 01 2019
STATUS
approved