OFFSET
0,3
COMMENTS
Reversing binary value of n: convert sum of powers of 2 in binary representation of n to alternating sum.
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).
REFERENCES
Donald E. Knuth, The Art of Computer Programming. Addison-Wesley, Reading, MA, 1969, Vol. 2, p. 178 (exercise 4.1. Nr. 27).
LINKS
N. J. A. Sloane, Table of n, a(n) for n = 0..20000 (First 8192 terms from Reinhard Zumkeller)
FORMULA
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.
From Antti Karttunen, Aug 18 2014: (Start)
a(A065621(n)) = n.
a(A048724(n)) = -n.
(End)
a(n) = -A104895(n). - M. F. Hasler, Apr 16 2018
EXAMPLE
11 = 1 + 2 + 8 -> 1 - 2 + 8 = 7 = a(11).
MAPLE
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;
[seq(f(n), n=0..100)]; # N. J. A. Sloane, Apr 25 2018
MATHEMATICA
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 *)
PROG
(Haskell)
import Data.List (transpose)
a065620 n = a065620_list !! n
a065620_list = 1 : concat (transpose [zs, map ((+ 1) . negate) zs])
where zs = map (* 2) a065620_list
-- Reinhard Zumkeller, Mar 26 2014
(Scheme)
(definec (A065620 n) (cond ((zero? n) n) ((even? n) (* 2 (A065620 (/ n 2)))) (else (+ 1 (* -2 (A065620 (/ (- n 1) 2)))))))
;; using memoization-macro definec from IntSeq-library of Antti Karttunen, Aug 18 2014
(Python)
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
(Python)
def A065620(n):
c, a, b = 0, -1, 1
for j in bin(n)[-1:1:-1]:
if int(j):
c += (a:=-a)*b
b <<= 1
return c # Chai Wah Wu, Sep 19 2023
(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
CROSSREFS
KEYWORD
AUTHOR
Marc LeBrun, Nov 07 2001
EXTENSIONS
Formula fixed by Reinhard Zumkeller, Mar 26 2014
Extended to a(0) = 0 by M. F. Hasler, Apr 16 2018
Edited by N. J. A. Sloane, Apr 25 2018
STATUS
approved