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.

A344985 Sum of imbalance of all initial subsequences of the binary representation of n.

Original entry on oeis.org

1, 1, 3, 2, 2, 4, 6, 4, 2, 2, 4, 4, 6, 8, 10, 7, 5, 3, 3, 3, 3, 5, 7, 5, 5, 7, 9, 9, 11, 13, 15, 11, 9, 7, 5, 5, 3, 3, 5, 5, 3, 3, 5, 5, 7, 9, 11, 7, 5, 5, 7, 7, 9, 11, 13, 9, 11, 13, 15, 15, 17, 19, 21, 16, 14, 12, 10, 10, 8, 6, 6, 8, 6, 4, 4, 4, 4, 6, 8, 8
Offset: 1

Views

Author

André Engels, Jun 04 2021

Keywords

Comments

The imbalance of a sequence of zeros and ones is given as the absolute value of the difference between the number of zeros and the number of ones (that is, the absolute value of A037861 if n is the sequence A007088).

Examples

			9 in binary is 1001. The imbalance of 1 is 1, the imbalance of 10 is 0, the imbalance of 100 is 1 and the imbalance of 1001 is 0. thus a(9)=1+0+1+0=2.
		

Crossrefs

Programs

  • Maple
    imbal := proc(L)
         abs(numboccur(0, L) - numboccur(1, L))
    end proc:
    A344985 := proc(n)
        dgs := ListTools[Reverse](convert(n,base,2)) ;
        add(imbal( dgs[1..l]),l=1..nops(dgs)) ;
    end proc: # R. J. Mathar, Jul 07 2021
    # second Maple program:
    a:= proc(n) option remember; `if`(n=0, 0, a(iquo(n, 2))+
          (l-> abs(add(1-2*i, i=l)))(Bits[Split](n)))
        end:
    seq(a(n), n=1..80);  # Alois P. Heinz, Jul 07 2021
  • PARI
    a(n) = my(b=binary(n)); sum(k=1, #b, abs(sum(j=1, k, b[j]==1) - sum(j=1, k, b[j]==0))); \\ Michel Marcus, Jun 09 2021
    
  • Python
    def A344985(n):
        s, c, b = bin(n)[2:], 0, 0
        for x in s:
            b += 1 if x == '1' else -1
            c += abs(b)
        return c # Chai Wah Wu, Jul 07 2021

Formula

a(n) = a(floor(n/2)) + |A037861(n)|. - R. J. Mathar, Jul 07 2021