from sympy.ntheory.factor_ import digits def a004488(n): return int("".join([str((3 - i)%3) for i in digits(n, 3)[1:]]), 3) def a117966(n): if n==0: return 0 if n%3==0: return 3*a117966(n/3) elif n%3==1: return 3*a117966((n - 1)/3) + 1 else: return 3*a117966((n - 2)/3) - 1 def a117968(n): if n==1: return 2 if n%3==0: return 3*a117968(n/3) elif n%3==1: return 3*a117968((n - 1)/3) + 2 else: return 3*a117968((n + 1)/3) + 1 def a117967(n): return 0 if n==0 else a117968(-n) if n<0 else a004488(a117968(n)) def a(n): if n<2: return n x=a117966(n) if x<0: return a117967(1 + a(-x)) else: return a117968(a(x - 1)) print [a(n) for n in xrange(101)]