cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A065363 Sum of balanced ternary digits in n. Replace 3^k with 1 in balanced ternary expansion of n.

This page as a plain text file.
%I A065363 #87 Jan 07 2025 08:33:06
%S A065363 0,1,0,1,2,-1,0,1,0,1,2,1,2,3,-2,-1,0,-1,0,1,0,1,2,-1,0,1,0,1,2,1,2,3,
%T A065363 0,1,2,1,2,3,2,3,4,-3,-2,-1,-2,-1,0,-1,0,1,-2,-1,0,-1,0,1,0,1,2,-1,0,
%U A065363 1,0,1,2,1,2,3,-2,-1,0,-1,0,1,0,1,2,-1,0,1,0,1,2,1,2,3,0,1,2,1,2,3,2,3,4,-1,0,1,0,1,2,1,2,3,0,1,2,1,2
%N A065363 Sum of balanced ternary digits in n. Replace 3^k with 1 in balanced ternary expansion of n.
%C A065363 Notation: (3)<n>(1).
%C A065363 Extension to negative n: a(-n) = -a(n). - _Franklin T. Adams-Watters_, May 13 2009
%C A065363 Row sums of A059095. - _Rémy Sigrist_, Oct 05 2019
%H A065363 Reinhard Zumkeller, <a href="/A065363/b065363.txt">Table of n, a(n) for n = 0..10000</a>
%H A065363 F. T. Adams-Watters and F. Ruskey, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL12/Ruskey2/ruskey14.html">Generating Functions for the Digital Sum and Other Digit Counting Sequences</a>, JIS 12 (2009) 09.5.6.
%H A065363 Daniel Forgues, <a href="/A065363/a065363.txt">Table of n, a(n) for n = 0..100000</a>
%F A065363 G.f.: (1/(1-x))*Sum_{k>=0} (x^3^k - x^(2*3^k))/(x^((3^k-1)/2)*(1 + x^3^k + x^(2*3^k))). - _Franklin T. Adams-Watters_, May 13 2009
%F A065363 a(n) = A134024(n) - A134022(n). - _Reinhard Zumkeller_, Dec 16 2010
%F A065363 a(3*n - 1) = a(n) - 1, a(3*n) = a(n), a(3*n + 1) = a(n) + 1. - _Thomas König_, Jun 24 2020
%F A065363 a(n) = A053735(2n) - A053735(n). This can be shown with constructing balanced ternary representation of n: Add n and n with carry, and then subtract n from the sum without borrow. - _Yifan Xie_, Dec 24 2024
%e A065363 5 = + 1(9) - 1(3) - 1(1) -> +1 - 1 - 1 = -1 = a(5).
%p A065363 a:= proc(n) `if`(n=0, 0, (d-> `if`(d=2,
%p A065363       a(q+1)-1, d+a(q)))(irem(n, 3, 'q')))
%p A065363     end:
%p A065363 seq(a(n), n=0..120);  # _Alois P. Heinz_, Jan 09 2020
%t A065363 balTernDigits[0] := {0}; balTernDigits[n_/;n > 0] := Module[{unParsed = n, currRem, currExp = 1, digitList = {}, nextDigit}, While[unParsed > 0, If[unParsed == 3^(currExp - 1), digitList = Append[digitList, 1]; unParsed = 0, currRem = Mod[unParsed, 3^currExp]/3^(currExp - 1); nextDigit = Switch[currRem, 0, 0, 2, -1, 1, 1]; digitList = Append[digitList, nextDigit]; unParsed = unParsed - nextDigit * 3^(currExp - 1)]; currExp++]; digitList = Reverse[digitList]; Return[digitList]]; balTernDigits[n_/;n < 0] := (-1)balTernDigits[Abs[n]]; Table[Plus@@balTernDigits[n], {n, 0, 108}] (* _Alonso del Arte_, Feb 25 2011 *)
%t A065363 terVal[lst_List] := Reverse[lst].(3^Range[0, Length[lst] - 1]); maxDig = 4; t = Table[0, {3 * 3^maxDig/2}]; t[[1]] = 1; Do[d = IntegerDigits[Range[0, 3^dig - 1], 3, dig]/.{2 -> -1}; d = Prepend[#, 1]&/@d; t[[terVal/@d]] = Total/@d, {dig, maxDig}]; Prepend[t, 0] (* _T. D. Noe_, Feb 24 2011 *)
%t A065363 Array[Total[Prepend[IntegerDigits[#, 3], 0] //. {a___, b_, 2, c___} :> {a, b + 1, -1, c}] &, 109, 0] (* _Michael De Vlieger_, Jun 27 2020 *)
%o A065363 (Python)
%o A065363 def a(n):
%o A065363     s=0
%o A065363     x=0
%o A065363     while n>0:
%o A065363         x=n%3
%o A065363         n=n//3
%o A065363         if x==2:
%o A065363             x=-1
%o A065363             n+=1
%o A065363         s+=x
%o A065363     return s
%o A065363 print([a(n) for n in range(101)]) # _Indranil Ghosh_, Jun 06 2017
%o A065363 (PARI) bt(n)=my(d=digits(n,3),c=1); while(c, if(d[1]==2, d=concat(0,d)); c=0; for(i=2,#d, if(d[i]==2,d[i]=-1;d[i-1]+=1;c=1))); d
%o A065363 a(n)=vecsum(bt(n)) \\ _Charles R Greathouse IV_, May 07 2020
%Y A065363 Cf. A059095, A065364, A053735. See A134452 for iterations.
%Y A065363 Cf. A134022, A134024.
%K A065363 base,easy,sign,look
%O A065363 0,5
%A A065363 _Marc LeBrun_, Oct 31 2001