OFFSET
0,2
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers.
LINKS
EXAMPLE
The ternary expansion of 321 is "102220", so the ternary expansion of a(321) is "101210", and a(321) = 291.
PROG
(PARI) a(n) = { my (d = digits(n, 3), nz = 0); forstep (k = #d, 1, -1, if (d[k], if (nz++%2==1, d[k] = 3-d[k]; ); ); ); fromdigits(d, 3); }
(Python)
from gmpy2 import digits
def a(n):
d, nz = list(digits(n, 3)), 0
for i, di in enumerate(d[::-1], 1):
if di != '0':
nz += 1
if nz&1: d[-i] = '2' if di == '1' else '1'
return int("".join(d), 3)
print([a(n) for n in range(68)]) # Michael S. Branicky, Jan 24 2025
CROSSREFS
KEYWORD
nonn,base,easy
AUTHOR
Rémy Sigrist, Jan 22 2025
STATUS
approved