A145037 Number of 1's minus number of 0's in the binary representation of n.
0, 1, 0, 2, -1, 1, 1, 3, -2, 0, 0, 2, 0, 2, 2, 4, -3, -1, -1, 1, -1, 1, 1, 3, -1, 1, 1, 3, 1, 3, 3, 5, -4, -2, -2, 0, -2, 0, 0, 2, -2, 0, 0, 2, 0, 2, 2, 4, -2, 0, 0, 2, 0, 2, 2, 4, 0, 2, 2, 4, 2, 4, 4, 6, -5, -3, -3, -1, -3, -1, -1, 1, -3, -1, -1, 1, -1, 1, 1, 3, -3, -1, -1, 1, -1, 1, 1, 3, -1, 1, 1
Offset: 0
Examples
From _Michel Marcus_, Feb 12 2022: (Start) Viewed as an irregular triangle: 0; 1; 0, 2; -1, 1, 1, 3; -2, 0, 0, 2, 0, 2, 2, 4; -3, -1, -1, 1, -1, 1, 1, 3, -1, 1, 1, 3, 1, 3, 3, 5; ... (End)
Crossrefs
Programs
-
Haskell
a145037 0 = 0 a145037 n = a145037 n' + 2*m - 1 where (n', m) = divMod n 2 -- Reinhard Zumkeller, Jun 16 2011
-
Maple
a:= n-> add(2*i-1, i=Bits[Split](n)): seq(a(n), n=0..90); # Alois P. Heinz, Jan 18 2022
-
Mathematica
Join[{0}, Table[Count[#, 1] - Count[#, 0] &[IntegerDigits[n, 2]], {n, 1, 90}]] (* Robert P. P. McKone, Feb 12 2022 *)
-
PARI
A145037(n)=hammingweight(n)*2-logint(n<<1+!n,2) \\ M. F. Hasler, Mar 08 2018
-
Python
result = [0] for n in range (1, 2**14 + 1): result.append(bin(n)[2:].count("1") - bin(n)[2:].count("0")) print(result[0:129]) # Karl-Heinz Hofmann, Jan 18 2022
-
Python
def a(n): return (n.bit_count()<<1) - n.bit_length() print([a(n) for n in range(1, 2**14+1)]) # Michael S. Branicky, May 14 2024 (C#) int A145037(int n) { int result = 0; while(n > 0) { result += 2 * (n % 2) - 1; n /= 2; } return result; } \\ Frank Hollstein, Dec 08 2022
Formula
a(n) = -A037861(n) for n >= 1.
a(n) = Sum_{i=1..k} (2*b[i] - 1) where b is the binary expansion of n and k is the number of bits in this binary expansion. - Michel Marcus, Jun 28 2021
From Aayush Soni Feb 12 2022: (Start)
Upper bound: a(n) <= floor(log_2(n+1)).
Lower bound: For n > 0, a(n) >= 1 - floor(log_2(n)).
If n is even, a(2^n) to a(2^(n+1)-1) inclusive are all odd and vice versa. (End)
Extensions
Renamed (using a Mar 08 2018 comment from M. F. Hasler) and edited by Jon E. Schoenfield, Jun 29 2021
Comments