login

Year-end appeal: Please make a donation to the OEIS Foundation to support ongoing development and maintenance of the OEIS. We are now in our 61st year, we have over 378,000 sequences, and we’ve reached 11,000 citations (which often say “discovered thanks to the OEIS”).

A301336
a(n) = total number of 1's minus total number of 0's in binary expansions of 0, ..., n.
4
-1, 0, 0, 2, 1, 2, 3, 6, 4, 4, 4, 6, 6, 8, 10, 14, 11, 10, 9, 10, 9, 10, 11, 14, 13, 14, 15, 18, 19, 22, 25, 30, 26, 24, 22, 22, 20, 20, 20, 22, 20, 20, 20, 22, 22, 24, 26, 30, 28, 28, 28, 30, 30, 32, 34, 38, 38, 40, 42, 46, 48, 52, 56, 62, 57, 54, 51, 50, 47, 46, 45, 46, 43, 42, 41, 42
OFFSET
0,4
FORMULA
G.f.: -1/(1 - x) + (1/(1 - x)^2)*Sum_{k>=0} x^(2^k)*(1 - x^(2^k))/(1 + x^(2^k)).
a(n) = A000788(n) - A059015(n).
a(n) = A268289(n) - 1.
a(A000079(n)) = A000295(n).
EXAMPLE
+---+-----+---+---+---+---+------------+
| n | bin.|1's|sum|0's|sum| a(n) |
+---+-----+---+---+---+---+------------+
| 0 | 0 | 0 | 0 | 1 | 1 | 0 - 1 =-1 |
| 1 | 1 | 1 | 1 | 0 | 1 | 1 - 1 = 0 |
| 2 | 10 | 1 | 2 | 1 | 2 | 2 - 2 = 0 |
| 3 | 11 | 2 | 4 | 0 | 2 | 4 - 2 = 2 |
| 4 | 100 | 1 | 5 | 2 | 4 | 5 - 4 = 1 |
| 5 | 101 | 2 | 7 | 1 | 5 | 7 - 5 = 2 |
| 6 | 110 | 2 | 9 | 1 | 6 | 9 - 6 = 3 |
+---+-----+---+---+---+---+------------+
bin. - n written in base 2;
1's - number of 1's in binary expansion of n;
0's - number of 0's in binary expansion of n;
sum - total number of 1's (or 0's) in binary expansions of 0, ..., n.
MAPLE
a:= proc(n) option remember; `if`(n=0, -1,
a(n-1)+add(2*i-1, i=Bits[Split](n)))
end:
seq(a(n), n=0..75); # Alois P. Heinz, Nov 11 2024
MATHEMATICA
Accumulate[DigitCount[Range[0, 75], 2, 1] - DigitCount[Range[0, 75], 2, 0]]
PROG
(Python)
def A301336(n):
return sum(2*bin(i).count('1')-len(bin(i))+2 for i in range(n+1)) # Chai Wah Wu, Sep 03 2020
(Python)
def A301336(n): return (n+1)*((n.bit_count()<<1)-(t:=(n+1).bit_length()))+(1<<t)+sum((m:=1<<j)*((k:=n>>j)-(r if n<<1>=m*(r:=k<<1|1) else 0)) for j in range(1, n.bit_length()+1))-2 # Chai Wah Wu, Nov 11 2024
KEYWORD
sign,base
AUTHOR
Ilya Gutkovskiy, Mar 28 2018
STATUS
approved