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.

Showing 1-5 of 5 results.

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

Original entry on oeis.org

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, 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, 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
Offset: 0

Views

Author

Marc LeBrun, Oct 31 2001

Keywords

Comments

Notation: (3)(1).
Extension to negative n: a(-n) = -a(n). - Franklin T. Adams-Watters, May 13 2009
Row sums of A059095. - Rémy Sigrist, Oct 05 2019

Examples

			5 = + 1(9) - 1(3) - 1(1) -> +1 - 1 - 1 = -1 = a(5).
		

Crossrefs

Cf. A059095, A065364, A053735. See A134452 for iterations.

Programs

  • Maple
    a:= proc(n) `if`(n=0, 0, (d-> `if`(d=2,
          a(q+1)-1, d+a(q)))(irem(n, 3, 'q')))
        end:
    seq(a(n), n=0..120);  # Alois P. Heinz, Jan 09 2020
  • Mathematica
    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 *)
    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 *)
    Array[Total[Prepend[IntegerDigits[#, 3], 0] //. {a___, b_, 2, c___} :> {a, b + 1, -1, c}] &, 109, 0] (* Michael De Vlieger, Jun 27 2020 *)
  • 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
    a(n)=vecsum(bt(n)) \\ Charles R Greathouse IV, May 07 2020
  • Python
    def a(n):
        s=0
        x=0
        while n>0:
            x=n%3
            n=n//3
            if x==2:
                x=-1
                n+=1
            s+=x
        return s
    print([a(n) for n in range(101)]) # Indranil Ghosh, Jun 06 2017
    

Formula

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
a(n) = A134024(n) - A134022(n). - Reinhard Zumkeller, Dec 16 2010
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
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

A134021 Length of n in balanced ternary representation.

Original entry on oeis.org

1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
Offset: 0

Views

Author

Reinhard Zumkeller, Oct 19 2007

Keywords

Comments

Shifted variant of A064099.

Examples

			100 = 1*3^4+1*3^3-1*3^2+0*3^1+1*3^0: a(100) = |++-0+| = 5.
200 = 1*3^5-1*3^4+1*3^3+1*3^2+1*3^1-1*3^0: a(200) = |+-+++-| = 6.
300 = 1*3^5+1*3^4-1*3^3+0*3^2+1*3^1+0*3^0: a(300) = |++-0+0| = 6.
		

References

  • Donald E. Knuth, The Art of Computer Programming, Addison-Wesley, Reading, MA, Vol. 2, pp. 173-175.

Crossrefs

Programs

  • Mathematica
    a[n_] := Ceiling[Log[3, 2*n+1]]; a[0] = 1; Array[a, 100, 0] (* Amiram Eldar, Apr 03 2025 *)
  • Python
    def a(n):
        if n==0: return 1
        s=0
        x=0
        while n>0:
            x=n%3
            n=n//3
            if x==2:
                x=-1
                n+=1
            s+=1
        return s
    print([a(n) for n in range(101)]) # Indranil Ghosh, Jun 07 2017

Formula

For n > 0: a(n) = ceiling(log(2*n+1)/log(3)).
a(n) = A134022(n) + A134023(n) + A134024(n).
0 <= a(n) - A081604(n) <= 1.
a(A134025(n)) = A081604(A134025(n)); a(A134026(n)) = A081604(A134026(n))+1.
a(A134027(n)) = a(n); a(abs(A134028(n))) <= a(n).
a(n) = A064099(n-1) for n>1.
n = Sum_{k=0..a(n)-1} (A059095(A134421(n)-2-k)*3^k), for n > 0. - Reinhard Zumkeller, Oct 25 2007
a(n) = A005812(n) + A134023(n).

A005812 Weight of balanced ternary representation of n.

Original entry on oeis.org

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, 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, 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
Offset: 0

Views

Author

Keywords

Comments

Weight of n means count of nonzero digits of n. - Daniel Forgues, Mar 24 2010
a(n) = A134022(n) + A134024(n) = A134021(n) - A134023(n).

References

  • N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).

Programs

  • Lisp
    (defun btw (n) (if (= n 0) 0 (multiple-value-bind (q r) (round n 3) (+ (abs r) (btw q)))))
    
  • Mathematica
    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 *)
  • PARI
    a(n)=local(q); if(n<=0,0,q=round(n/3); abs(n-3*q)+a(q))
    
  • Python
    def a(n):
        s=0
        x=0
        while n>0:
            x=n%3
            n//=3
            if x==2:
                x=-1
                n+=1
            if x!=0: s+=1
        return s
    print([a(n) for n in range(101)]) # Indranil Ghosh, Jun 07 2017

Formula

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).
a(n) = Sum_{k>0} floor(|2*sin(n*Pi/3^k)|). - Toshitaka Suzuki, Sep 10 2006

Extensions

Additional terms from Allan C. Wechsler

A134023 Number of zeros in balanced ternary representation of n.

Original entry on oeis.org

1, 0, 0, 1, 0, 0, 1, 0, 1, 2, 1, 0, 1, 0, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 2, 3, 2, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 2, 3, 2, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 2, 3, 2, 1, 2, 1, 2, 3, 2, 3, 4, 3, 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1
Offset: 0

Views

Author

Reinhard Zumkeller, Oct 19 2007

Keywords

Examples

			100=1*3^4+1*3^3-1*3^2+0*3^1+1*3^0=='++-0+': a(100)=1;
200=1*3^5-1*3^4+1*3^3+1*3^2+1*3^1-1*3^0=='+-+++-': a(200)=0;
300=1*3^5+1*3^4-1*3^3+0*3^2+1*3^1+0*3^0=='++-0+0': a(300)=2.
		

References

  • D. E. Knuth, The Art of Computer Programming, Addison-Wesley, Reading, MA, Vol 2, pp 173-175.

Crossrefs

Programs

  • Mathematica
    Array[Count[If[First@ # == 0, Rest@ #, #], 0] &[Prepend[IntegerDigits[#, 3], 0] //. {a___, b_, 2, c___} :> {a, b + 1, -1, c}] &, 105, 0] (* Michael De Vlieger, Jun 27 2020 *)
  • Python
    def a(n):
        if n==0: return 1
        s=0
        x=0
        while n>0:
            x=n%3
            n=n//3
            if x==2:
                x=-1
                n+=1
            if x==0: s+=1
        return s
    print([a(n) for n in range(101)]) # Indranil Ghosh, Jun 07 2017

Formula

a(n) = A134021(n) - A134022(n) - A134024(n).
a(n) = A134021(n) - A005812(n).

A134022 Number of negative trits in balanced ternary representation of n.

Original entry on oeis.org

0, 0, 1, 0, 0, 2, 1, 1, 1, 0, 0, 1, 0, 0, 3, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 0, 0, 1, 0, 0, 2, 1, 1, 1, 0, 0, 1, 0, 0, 4, 3, 3, 3, 2, 2, 3, 2, 2, 3, 2, 2, 2, 1, 1, 2, 1, 1, 3, 2, 2, 2, 1, 1, 2, 1, 1, 3, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 0, 0, 1, 0, 0, 2, 1, 1, 1, 0, 0, 1, 0, 0, 3, 2, 2, 2, 1, 1, 2, 1, 1, 2
Offset: 0

Views

Author

Reinhard Zumkeller, Oct 19 2007

Keywords

Examples

			100 = 1*3^4+1*3^3-1*3^2+0*3^1+1*3^0 == '++-0+': a(100) = 1;
200 = 1*3^5-1*3^4+1*3^3+1*3^2+1*3^1-1*3^0 == '+-+++-': a(200) = 2;
300 = 1*3^5+1*3^4-1*3^3+0*3^2+1*3^1+0*3^0 == '++-0+0': a(300) = 1.
		

References

  • D. E. Knuth, The Art of Computer Programming, Addison-Wesley, Reading, MA, Vol 2, pp 173-175.

Crossrefs

Programs

  • Mathematica
    Array[Count[#, -1] &[Prepend[IntegerDigits[#, 3], 0] //. {a___, b_, 2, c___} :> {a, b + 1, -1, c}] &, 105, 0] (* Michael De Vlieger, Jun 27 2020 *)
  • Python
    def a(n):
        s=0
        x=0
        while n>0:
            x=n%3
            n=n//3
            if x==2:
                x=-1
                n+=1
            if x==-1: s+=1
        return s
    print([a(n) for n in range(151)]) # Indranil Ghosh, Jun 07 2017

Formula

a(n) = A134021(n) - A134023(n) - A134024(n).
a(n) = A005812(n) - A134024(n) = A134024(n) - A065363(n).
Showing 1-5 of 5 results.