OFFSET
0,3
COMMENTS
This sequence is a self-inverse permutation of nonnegative integers.
LINKS
FORMULA
a(3 * n) = 3 * a(n).
EXAMPLE
For n = 3497:
- the ternary representation of 3497 is "11210112",
- we replace "1121" by "1211" and "112" by "211" and obtain "12110211",
- hence a(3497) = 3991.
MATHEMATICA
rernz[n_]:=FromDigits[Flatten[If[FreeQ[#, 0], Reverse[#], #]&/@SplitBy[ IntegerDigits[ n, 3], #!=0&]], 3]; Array[rernz, 70, 0] (* Harvey P. Dale, Nov 15 2020 *)
PROG
(PARI) a(n, base=3) = my (d=digits(n*base, base), nz=0); for (i=1, #d, if (d[i], nz++, if (nz, for (j=1, floor(nz/2), [d[i-j], d[i-nz-1+j]] = [d[i-
nz-1+j], d[i-j]]); nz=0))); fromdigits(d, base)/base
CROSSREFS
KEYWORD
AUTHOR
Rémy Sigrist, Nov 17 2018
STATUS
approved