%I #24 Jul 19 2024 08:48:11
%S 0,1,2,-1,0,1,-2,-1,0,1,2,3,0,1,2,-1,0,1,2,3,4,1,2,3,0,1,2,-1,0,1,-2,
%T -1,0,-3,-2,-1,0,1,2,-1,0,1,-2,-1,0,1,2,3,0,1,2,-1,0,1,-2,-1,0,-3,-2,
%U -1,-4,-3,-2,-1,0,1,-2,-1,0,-3,-2,-1,0,1,2,-1,0,1,-2,-1,0,1,2,3,0,1,2,-1,0,1,2,3,4,1,2,3,0,1,2,3,4,5,2,3
%N Alternating sum of ternary digits in n. Replace 3^k with (-1)^k in ternary expansion of n.
%C Notation: (3)[n](-1).
%C Fixed point of the morphism 0 -> 0,1,2; 1 -> -1,0,1; 2 -> -2,-1,0; ...; n -> -n,-n+1,-n+2. - _Philippe Deléham_, Oct 22 2011
%F a(n) = Sum_{k>=0} A030341(n,k)*(-1)^k. - _Philippe Deléham_, Oct 22 2011.
%F G.f. A(x) satisfies: A(x) = x * (1 + 2*x) / (1 - x^3) - (1 + x + x^2) * A(x^3). - _Ilya Gutkovskiy_, Jul 28 2021
%e 15 = +1(9)+2(3)+0(1) -> +1(+1)+2(-1)+0(+1) = -1 = a(15).
%o (Python)
%o from sympy.ntheory.digits import digits
%o def a(n):
%o return sum(bi*(-1)**k for k, bi in enumerate(digits(n, 3)[1:][::-1]))
%o print([a(n) for n in range(104)]) # _Michael S. Branicky_, Jul 28 2021
%o (Python)
%o from sympy.ntheory import digits
%o def A065368(n): return sum((0, 1, 2, -1, 0, 1, -2, -1, 0)[i] for i in digits(n,9)[1:]) # _Chai Wah Wu_, Jul 19 2024
%Y Cf. A030341, A053735, A065359, A065364.
%K base,easy,sign
%O 0,3
%A _Marc LeBrun_, Oct 31 2001
%E Initial 0 added by _Philippe Deléham_, Oct 22 2011