OFFSET
1,6
LINKS
Mark Povich, Table of n, a(n) for n = 1..9999
noyonict, Number conversion in Python 3
EXAMPLE
When n=18, a(n) = 0. Convert 18 to binary (=10010). The 1s are in the 2^4 place and the 2^1 place. Take those exponents and add them (=5). The 0's are in the 2^3, 2^2, and 2^0 places. Subtract those exponents (=5) from the previous sum to get 0.
When n=26, a(n) = 6. Convert 26 to binary (=11010). The 1s are in the 2^4, 2^3, and 2^1 places. Take those exponents and add them (=8). The 0's are in the 2^2 and 2^0 places. Subtract those exponents (=2) from the previous sum to get 6.
MATHEMATICA
a[n_] := Block[{d = Reverse@IntegerDigits[n, 2]}, Total@ Flatten@ {Position[d, 1]-1, 1-Position[d, 0]}]; Array[a, 74] (* Giovanni Resta, Aug 26 2019 *)
PROG
(Python)
def dec_to_bin(dec_num): #define a function that converts decimal to binary.
bin_num = 0
power = 0
while dec_num > 0:
bin_num += 10 ** power * (dec_num % 2)
dec_num //= 2
power += 1
return bin_num
def rev_bin(n):
return list(reversed(str(dec_to_bin(n))))
n = 18
neg = [pos for pos, num in enumerate(rev_bin(n)) if num == "0"]
posi = [pos for pos, num in enumerate(rev_bin(n)) if num == "1"]
print(sum(posi)-sum(neg))
(PARI) a(n) = {my(b=Vecrev(binary(n))); my(v1 = Vec(select(x->(x==1), b, 1))); my(v0 = Vec(select(x->(x==0), b, 1))); (vecsum(v1) - #v1) - (vecsum(v0) - #v0); } \\ Michel Marcus, Aug 26 2019
(Python)
def A309983(n):
r, i = 0, 0
while n > 0:
d, n = n%2, n//2
if d == 1:
r = r+i
else:
r = r-i
i = i+1
return r # A.H.M. Smeets, Oct 07 2019
CROSSREFS
KEYWORD
sign,base
AUTHOR
Mark Povich, Aug 26 2019
STATUS
approved