|
|
A145037
|
|
Number of 1's minus number of 0's in the binary representation of n.
|
|
26
|
|
|
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
(list;
graph;
refs;
listen;
history;
text;
internal format)
|
|
|
OFFSET
|
0,4
|
|
COMMENTS
|
Column 2 of A144912 (which begins at n = 2).
Zeros in that column correspond to A031443.
|
|
LINKS
|
|
|
FORMULA
|
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
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)
|
|
EXAMPLE
|
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)
|
|
MAPLE
|
a:= n-> add(2*i-1, i=Bits[Split](n)):
|
|
MATHEMATICA
|
Join[{0}, Table[Count[#, 1] - Count[#, 0] &[IntegerDigits[n, 2]], {n, 1, 90}]] (* Robert P. P. McKone, Feb 12 2022 *)
|
|
PROG
|
(Haskell)
a145037 0 = 0
a145037 n = a145037 n' + 2*m - 1 where (n', m) = divMod n 2
(Python)
result = [0]
for n in range (1, 2**14 + 1):
result.append(bin(n)[2:].count("1") - bin(n)[2:].count("0"))
(Python)
def a(n): return (n.bit_count()<<1) - n.bit_length()
(C#)
int result = 0;
while(n > 0) {
result += 2 * (n % 2) - 1;
n /= 2;
}
return result;
|
|
CROSSREFS
|
|
|
KEYWORD
|
sign,base,easy
|
|
AUTHOR
|
|
|
EXTENSIONS
|
|
|
STATUS
|
approved
|
|
|
|