Year-end appeal: Please make a donation to the OEIS Foundation to support ongoing development and maintenance of the OEIS. We are now in our 61st year, we have over 378,000 sequences, and we’ve reached 11,000 citations (which often say “discovered thanks to the OEIS”).
%I #41 Sep 20 2019 10:49:41
%S 0,1,-1,1,4,-2,-1,2,-4,1,10,-8,4,13,-5,-2,7,-11,-1,8,-10,2,11,-7,-4,5,
%T -13,1,28,-26,10,37,-17,-8,19,-35,4,31,-23,13,40,-14,-5,22,-32,-2,25,
%U -29,7,34,-20,-11,16,-38,-1,26,-28,8,35,-19,-10,17,-37,2,29,-25,11
%N Balanced reversed ternary: Write n as ternary, reverse the order of the digits, then replace all 2's with (-1)'s.
%H Rémy Sigrist, <a href="/A327252/b327252.txt">Table of n, a(n) for n = 0..19683</a>
%e 5 in base 3 is 12; reversing the ternary gives 21; replacing the 2 with (-1) gives (-1),1 which is -2 in decimal.
%p a:= proc(n) local m, r; m, r:= n, 0;
%p while m>0 do m, r:= iquo(m, 3), 3*r+mods(m, 3) od; r
%p end:
%p seq(a(n), n=0..3^4-1); # _Alois P. Heinz_, Sep 17 2019
%t a[n_] := FromDigits[Reverse[IntegerDigits[n, 3]] /. {2 -> -1}, 3]; Array[a, 67, 0] (* _Giovanni Resta_, Sep 17 2019 *)
%o (Python)
%o def a(n):
%o ternary = []
%o i = math.floor(math.log(n,3))
%o while i >= 0:
%o for j in range (2, -1, -1):
%o if j*(3**i) <= n:
%o if (j==2): ternary.append(-1)
%o else: ternary.append(j)
%o n -= j*(3**i)
%o break
%o i -=1
%o for k in range (0, len(ternary)):
%o n += ternary[k]*(3**k)
%o return n
%o (PARI) a(n) = fromdigits(apply(d -> if (d==2, -1, d), Vecrev(digits(n,3))), 3) \\ _Rémy Sigrist_, Sep 15 2019
%Y Cf. A134028 (reversed balanced ternary, i.e. balancing the ternary, then reversing the ternary and transforming back into decimal), A117966 (balanced ternary enumeration).
%K sign,look,base
%O 0,5
%A _Elisabeth Zemack_, Sep 15 2019