OFFSET
1,3
COMMENTS
LINKS
Alois P. Heinz, Table of n, a(n) for n = 1..16384
FORMULA
a(n) = a(floor(n/2)) + |A037861(n)|. - R. J. Mathar, Jul 07 2021
EXAMPLE
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.
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
PROG
(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
CROSSREFS
KEYWORD
AUTHOR
André Engels, Jun 04 2021
STATUS
approved