OFFSET
0,5
LINKS
Rémy Sigrist, Table of n, a(n) for n = 0..19683
EXAMPLE
5 in base 3 is 12; reversing the ternary gives 21; replacing the 2 with (-1) gives (-1),1 which is -2 in decimal.
MAPLE
a:= proc(n) local m, r; m, r:= n, 0;
while m>0 do m, r:= iquo(m, 3), 3*r+mods(m, 3) od; r
end:
seq(a(n), n=0..3^4-1); # Alois P. Heinz, Sep 17 2019
MATHEMATICA
a[n_] := FromDigits[Reverse[IntegerDigits[n, 3]] /. {2 -> -1}, 3]; Array[a, 67, 0] (* Giovanni Resta, Sep 17 2019 *)
PROG
(Python)
def a(n):
ternary = []
i = math.floor(math.log(n, 3))
while i >= 0:
for j in range (2, -1, -1):
if j*(3**i) <= n:
if (j==2): ternary.append(-1)
else: ternary.append(j)
n -= j*(3**i)
break
i -=1
for k in range (0, len(ternary)):
n += ternary[k]*(3**k)
return n
(PARI) a(n) = fromdigits(apply(d -> if (d==2, -1, d), Vecrev(digits(n, 3))), 3) \\ Rémy Sigrist, Sep 15 2019
CROSSREFS
KEYWORD
AUTHOR
Elisabeth Zemack, Sep 15 2019
STATUS
approved