%I #24 Apr 28 2021 10:10:56
%S 0,1,1,1,2,1,1,2,1,1,2,2,2,3,1,1,2,1,1,2,2,2,3,1,1,2,1,1,2,2,2,3,2,2,
%T 3,2,2,3,3,3,4,1,1,2,1,1,2,2,2,3,1,1,2,1,1,2,2,2,3,2,2,3,2,2,3,3,3,4,
%U 1,1,2,1,1,2,2,2,3,1,1,2,1,1,2,2,2,3,2,2,3,2,2,3,3,3,4,2,2,3,2,2,3,3,3,4,2
%N Number of positive trits in balanced ternary representation of n.
%D D. E. Knuth, The Art of Computer Programming, Addison-Wesley, Reading, MA, Vol 2, pp 173-175.
%H Reinhard Zumkeller, <a href="/A134024/b134024.txt">Table of n, a(n) for n = 0..10000</a>
%H Wikipedia, <a href="http://en.wikipedia.org/wiki/Balanced_ternary">Balanced Ternary</a>
%F a(n) = A134021(n) - A134022(n) - A134023(n);
%F a(n) > 0 for n > 0.
%F a(n) = A005812(n) - A134022(n) = A134022(n) + A065363(n).
%e 100=1*3^4+1*3^3-1*3^2+0*3^1+1*3^0=='++-0+': a(100)=3;
%e 200=1*3^5-1*3^4+1*3^3+1*3^2+1*3^1-1*3^0=='+-+++-': a(200)=4;
%e 300=1*3^5+1*3^4-1*3^3+0*3^2+1*3^1+0*3^0=='++-0+0': a(300)=3.
%t Array[Count[#, 1] &[Prepend[IntegerDigits[#, 3], 0] //. {a___, b_, 2, c___} :> {a, b + 1, -1, c}] &, 105, 0] (* _Michael De Vlieger_, Jun 27 2020 *)
%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==1: s+=1
%o return s
%o print([a(n) for n in range(151)]) # _Indranil Ghosh_, Jun 07 2017
%Y Cf. A005812, A059095, A065363, A134021, A134022, A134023.
%K nonn,base
%O 0,5
%A _Reinhard Zumkeller_, Oct 19 2007
|