%I M0111 #41 Apr 30 2021 06:07:00
%S 0,1,2,1,2,3,2,3,2,1,2,3,2,3,4,3,4,3,2,3,4,3,4,3,2,3,2,1,2,3,2,3,4,3,
%T 4,3,2,3,4,3,4,5,4,5,4,3,4,5,4,5,4,3,4,3,2,3,4,3,4,5,4,5,4,3,4,5,4,5,
%U 4,3,4,3,2,3,4,3,4,3,2,3,2,1,2,3,2,3,4,3,4,3,2,3,4,3,4,5,4,5,4,3,4,5,4,5,4,3
%N Weight of balanced ternary representation of n.
%C Weight of n means count of nonzero digits of n. - _Daniel Forgues_, Mar 24 2010
%C a(n) = A134022(n) + A134024(n) = A134021(n) - A134023(n).
%D N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
%H Daniel Forgues, <a href="/A005812/b005812.txt">Table of n, a(n) for n = 0..100000</a>
%H P. Flajolet and Lyle Ramshaw, <a href="http://dx.doi.org/10.1137/0209014">A note on Gray code and odd-even merge</a>, SIAM J. Comput. 9 (1980), 142-158.
%H Michael Gilleland, <a href="/selfsimilar.html">Some Self-Similar Integer Sequences</a>
%F a(3n)=a(n), a(3n+1)=a(n)+1, a(9n+2)=a(n)+2, a(9n+5)=a(3n+2)+1, a(9n+8)=a(3n+2).
%F a(n) = Sum_{k>0} floor(|2*sin(n*Pi/3^k)|). - _Toshitaka Suzuki_, Sep 10 2006
%t a[n_] := With[{q=Round[n/3]}, Abs[n-3q]+a[q]]; a[0]=0; Table[a[n], {n, 0, 105}](* _Jean-François Alcover_, Nov 25 2011, after Pari *)
%o (Lisp) (defun btw (n) (if (= n 0) 0 (multiple-value-bind (q r) (round n 3) (+ (abs r) (btw q)))))
%o (PARI) a(n)=local(q); if(n<=0,0,q=round(n/3); abs(n-3*q)+a(q))
%o (Python)
%o def a(n):
%o s=0
%o x=0
%o while n>0:
%o x=n%3
%o n//=3
%o if x==2:
%o x=-1
%o n+=1
%o if x!=0: s+=1
%o return s
%o print([a(n) for n in range(101)]) # _Indranil Ghosh_, Jun 07 2017
%K easy,nonn,nice
%O 0,3
%A _N. J. A. Sloane_, _Jeffrey Shallit_
%E Additional terms from _Allan C. Wechsler_