%I #43 Sep 19 2023 14:23:24
%S 0,1,2,-1,4,-3,-2,3,8,-7,-6,7,-4,5,6,-5,16,-15,-14,15,-12,13,14,-13,
%T -8,9,10,-9,12,-11,-10,11,32,-31,-30,31,-28,29,30,-29,-24,25,26,-25,
%U 28,-27,-26,27,-16,17,18,-17,20,-19,-18,19,24,-23,-22,23,-20,21,22,-21,64,-63,-62,63,-60,61,62,-61,-56
%N a(0)=0; thereafter a(2n) = 2a(n), a(2n+1) = -2a(n) + 1.
%C Reversing binary value of n: convert sum of powers of 2 in binary representation of n to alternating sum.
%C The alternation is applied only to the nonzero bits and does not depend on the exponent of 2. All integers have a unique reversing binary representation (see cited Knuth exercise for proof).
%D Donald E. Knuth, The Art of Computer Programming. Addison-Wesley, Reading, MA, 1969, Vol. 2, p. 178 (exercise 4.1. Nr. 27).
%H N. J. A. Sloane, <a href="/A065620/b065620.txt">Table of n, a(n) for n = 0..20000</a> (First 8192 terms from Reinhard Zumkeller)
%F a(n) = if n<2 then n else b+2*(1-2*b)*a((n-b)/2) where b is the least significant bit in n.
%F From _Antti Karttunen_, Aug 18 2014: (Start)
%F a(n) = A246160(n) - A246159(n).
%F a(A065621(n)) = n.
%F a(A048724(n)) = -n.
%F (End)
%F a(n) = -A104895(n). - _M. F. Hasler_, Apr 16 2018
%e 11 = 1 + 2 + 8 -> 1 - 2 + 8 = 7 = a(11).
%p f:=proc(n) option remember; if n=0 then 0 elif (n mod 2) = 0 then 2*f(n/2) else 1-2*f((n-1)/2); fi; end;
%p [seq(f(n),n=0..100)]; # _N. J. A. Sloane_, Apr 25 2018
%t a[0] = 0; a[n_]:= a[n]= If[OddQ[n], 1 - 2*a[(n-1)/2], 2*a[n/2]]; Array[a, 100, 0] (* _Amiram Eldar_, Sep 05 2023 *)
%o (Haskell)
%o import Data.List (transpose)
%o a065620 n = a065620_list !! n
%o a065620_list = 1 : concat (transpose [zs, map ((+ 1) . negate) zs])
%o where zs = map (* 2) a065620_list
%o -- _Reinhard Zumkeller_, Mar 26 2014
%o (Scheme)
%o (definec (A065620 n) (cond ((zero? n) n) ((even? n) (* 2 (A065620 (/ n 2)))) (else (+ 1 (* -2 (A065620 (/ (- n 1) 2)))))))
%o ;; using memoization-macro definec from IntSeq-library of _Antti Karttunen_, Aug 18 2014
%o (Python)
%o def a(n): return n if n<3 else 2*a(n/2) if n%2==0 else -2*a((n - 1)/2) + 1 # _Indranil Ghosh_, Jun 07 2017
%o (Python)
%o def A065620(n):
%o c, a, b = 0, -1, 1
%o for j in bin(n)[-1:1:-1]:
%o if int(j):
%o c += (a:=-a)*b
%o b <<= 1
%o return c # _Chai Wah Wu_, Sep 19 2023
%o (PARI) A065620(n,c=1)=sum(i=0,logint(n+!n,2),if(bittest(n,i),(-1)^c++<<i)) \\ _M. F. Hasler_, Apr 16 2018
%Y Cf. A065621, A048724, A246159, A246160.
%Y The negative of entry A104895.
%K base,easy,sign,look
%O 0,3
%A _Marc LeBrun_, Nov 07 2001
%E Formula fixed by _Reinhard Zumkeller_, Mar 26 2014
%E Extended to a(0) = 0 by _M. F. Hasler_, Apr 16 2018
%E Edited by _N. J. A. Sloane_, Apr 25 2018