login
A380351
In the ternary expansion of n, from right to left: replace the first, third, fifth, etc. nonzero digit, says d, by 3-d.
4
0, 2, 1, 6, 5, 4, 3, 8, 7, 18, 11, 10, 15, 23, 22, 12, 26, 25, 9, 20, 19, 24, 14, 13, 21, 17, 16, 54, 29, 28, 33, 59, 58, 30, 62, 61, 45, 65, 64, 69, 50, 49, 66, 53, 52, 36, 74, 73, 78, 41, 40, 75, 44, 43, 27, 56, 55, 60, 32, 31, 57, 35, 34, 72, 38, 37, 42, 77
OFFSET
0,2
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers.
FORMULA
a(A380352(n)) = A380352(a(n)) = A004488(n).
{a(n), A380352(n)} = {A380349(n), A380350(n)}.
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
See A380349, A380350 and A380352 for similar sequences.
Cf. A004488.
Sequence in context: A351385 A090665 A347952 * A376674 A361198 A359806
KEYWORD
nonn,base,easy
AUTHOR
Rémy Sigrist, Jan 22 2025
STATUS
approved