OFFSET
0,3
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers.
LINKS
FORMULA
EXAMPLE
The ternary expansion of 321 is "102220", so the ternary expansion of a(321) is "101210", and a(321) = 292.
PROG
(PARI) a(n) = { my (d = digits(n, 3), nz = 0); for (k = 1, #d, if (d[k], if (nz++%2==0, 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):
if di != '0':
nz += 1
if nz&1 == 0: 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,new
AUTHOR
Rémy Sigrist, Jan 22 2025
STATUS
approved