OFFSET
0,2
COMMENTS
The image of this sequence is the set of nonnegative even numbers (A005843).
EXAMPLE
For n = 42:
- the balanced ternary representation of 42 is "1TTT0",
- so a(42) = 42 + 1 - 1 - 1 - 1 + 0 = 40.
MATHEMATICA
Array[# + Total[If[First@ # == 0, Rest@ #, #] &[Prepend[IntegerDigits[#, 3], 0] //. {x___, y_, k_ /; k > 1, z___} :> {x, y + 1, k - 3, z}]] &, 70, 0] (* Michael De Vlieger, Jan 15 2022 *)
PROG
(PARI) a(n) = my (v=n, d); while (n, n=(n-d=[0, 1, -1][1+n%3])/3; v+=d); v
CROSSREFS
KEYWORD
nonn,base,easy
AUTHOR
Rémy Sigrist, Jan 09 2022
STATUS
approved