Reminder: The OEIS is hiring a new managing editor, and the application deadline is January 26.
%I #81 May 14 2024 06:10:49
%S 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,
%T 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,
%U 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
%N Number of 1's minus number of 0's in the binary representation of n.
%C Column 2 of A144912 (which begins at n = 2).
%C Zeros in that column correspond to A031443.
%H <a href="/A145037/b145037.txt">Table of n, a(n) for n = 0..2^14</a>
%F a(n) = -A037861(n) for n >= 1.
%F 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
%F From _Aayush Soni_ Feb 12 2022: (Start)
%F Upper bound: a(n) <= floor(log_2(n+1)).
%F Lower bound: For n > 0, a(n) >= 1 - floor(log_2(n)).
%F If n is even, a(2^n) to a(2^(n+1)-1) inclusive are all odd and vice versa. (End)
%e From _Michel Marcus_, Feb 12 2022: (Start)
%e Viewed as an irregular triangle:
%e 0;
%e 1;
%e 0, 2;
%e -1, 1, 1, 3;
%e -2, 0, 0, 2, 0, 2, 2, 4;
%e -3, -1, -1, 1, -1, 1, 1, 3, -1, 1, 1, 3, 1, 3, 3, 5;
%e ... (End)
%p a:= n-> add(2*i-1, i=Bits[Split](n)):
%p seq(a(n), n=0..90); # _Alois P. Heinz_, Jan 18 2022
%t Join[{0}, Table[Count[#, 1] - Count[#, 0] &[IntegerDigits[n, 2]], {n, 1, 90}]] (* _Robert P. P. McKone_, Feb 12 2022 *)
%o (Haskell)
%o a145037 0 = 0
%o a145037 n = a145037 n' + 2*m - 1 where (n', m) = divMod n 2
%o -- _Reinhard Zumkeller_, Jun 16 2011
%o (PARI) A145037(n)=hammingweight(n)*2-logint(n<<1+!n,2) \\ _M. F. Hasler_, Mar 08 2018
%o (Python)
%o result = [0]
%o for n in range (1, 2**14 + 1):
%o result.append(bin(n)[2:].count("1") - bin(n)[2:].count("0"))
%o print(result[0:129]) # _Karl-Heinz Hofmann_, Jan 18 2022
%o (Python)
%o def a(n): return (n.bit_count()<<1) - n.bit_length()
%o print([a(n) for n in range(1, 2**14+1)]) # _Michael S. Branicky_, May 14 2024
%o (C#)
%o int A145037(int n) {
%o int result = 0;
%o while(n > 0) {
%o result += 2 * (n % 2) - 1;
%o n /= 2;
%o }
%o return result;
%o } \\ _Frank Hollstein_, Dec 08 2022
%Y Cf. A037861 (negated), A031443 (indices of 0's), A144912, A000120.
%Y Cf. A269735 (first differences), A268289 (partial sums).
%Y Column k=1 of A360099.
%K sign,base,easy
%O 0,4
%A _Reikku Kulon_, Sep 30 2008
%E Renamed (using a Mar 08 2018 comment from _M. F. Hasler_) and edited by _Jon E. Schoenfield_, Jun 29 2021