OFFSET
0,3
COMMENTS
This sequence, together with its negative extension a(-n) = -a(n) is a self-inverse permutation of the integers. The absolute values are a self-inverse permutation of the nonnegative integers.
LINKS
Indranil Ghosh, Table of n, a(n) for n = 0..6561
FORMULA
EXAMPLE
87 in balanced ternary is 101(-1)0; leaving the final 0 and reversing the remaining digits gives (-1)1010, which is -51; so a(87) = -51.
PROG
(PARI) a(n)=local(r, dr, q); if(n==0, 0, r=0; dr=1; while(n%3==0, dr*=3; n\=3); while(n!=0, q=(n+1)\3; r=3*r+dr*(n-3*q); n=q); r) \\ Franklin T. Adams-Watters, May 24 2009
(Python)
def a(n):
if n==0: return 0
r=0
dr=1
while n%3==0:
dr*=3
n/=3
while n!=0:
q=(n + 1)/3
r=3*r + dr*(n - 3*q)
n=q
return r
##print [a(n) for n in range(101)] # Indranil Ghosh, Jun 10 2017, after Franklin T. Adams-Watters
CROSSREFS
KEYWORD
AUTHOR
Franklin T. Adams-Watters, May 21 2009
STATUS
approved