A327252 Balanced reversed ternary: Write n as ternary, reverse the order of the digits, then replace all 2's with (-1)'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, -13, 1, 28, -26, 10, 37, -17, -8, 19, -35, 4, 31, -23, 13, 40, -14, -5, 22, -32, -2, 25, -29, 7, 34, -20, -11, 16, -38, -1, 26, -28, 8, 35, -19, -10, 17, -37, 2, 29, -25, 11
Offset: 0
Examples
5 in base 3 is 12; reversing the ternary gives 21; replacing the 2 with (-1) gives (-1),1 which is -2 in decimal.
Links
- Rémy Sigrist, Table of n, a(n) for n = 0..19683
Crossrefs
Programs
-
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 *)
-
PARI
a(n) = fromdigits(apply(d -> if (d==2, -1, d), Vecrev(digits(n,3))), 3) \\ Rémy Sigrist, Sep 15 2019
-
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