login

Reminder: The OEIS is hiring a new managing editor, and the application deadline is January 26.

Reverse runs of ones in binary expansion of n and convert back to decimal.
2

%I #16 Dec 12 2018 15:41:14

%S 0,1,2,3,4,5,6,7,8,9,10,13,12,11,14,15,16,17,18,25,20,21,26,29,24,19,

%T 22,27,28,23,30,31,32,33,34,49,36,37,50,57,40,41,42,53,52,45,58,61,48,

%U 35,38,51,44,43,54,59,56,39,46,55,60,47,62,63,64,65,66,97

%N Reverse runs of ones in binary expansion of n and convert back to decimal.

%C This sequence is a self-inverse permutation of nonnegative integers.

%H Rémy Sigrist, <a href="/A322464/b322464.txt">Table of n, a(n) for n = 0..8192</a>

%H Rémy Sigrist, <a href="/A322464/a322464.png">Colored scatterplot of (n, a(n)) for n = 0..2^18-1</a> (where the color is function of A005811(n))

%H <a href="/index/Bi#binary">Index entries for sequences related to binary expansion of n</a>

%H <a href="/index/Per#IntegerPermutation">Index entries for sequences that are permutations of the natural numbers</a>

%F A000120(a(n)) = A000120(n).

%F A005811(a(n)) = A005811(n).

%F a(A322463(n)) = A322463(a(n)).

%F a(2^n) = 2^n.

%F a(2^n-1) = 2^n-1.

%e For n = 150:

%e - the binary representation of 150 is "10010110",

%e - we have three runs of ones: "1", "1" and "11",

%e - we exchange the first and the third run, and the second remains in place,

%e - we obtain: "11001010",

%e - hence a(150) = 202.

%t a[n_] := Module[{s=Split[IntegerDigits[n,2]]}, m=Length[s]; If[m>1, m2 = m-1 + Mod[m,2]; ind=Riffle[Range[m2,1,-2],Range[2,m,2]]; FromDigits[Flatten[ s[[ind]]],2], n]]; Array[a,120,0] (* _Amiram Eldar_, Dec 12 2018 *)

%o (PARI) a(n) = {

%o my (r=n, o=[], v=0, p=1, i=0);

%o while (r, my (l=valuation(r+(r%2),2)); if (r%2, o=concat(l,o)); r\=2^l);

%o while (n, my (l=valuation(n+(n%2),2)); if (n%2, v+=(2^o[i++]-1)*p; p*=2^o[i], p*=2^l); n\=2^l);

%o return (v);

%o }

%Y See A322463 for the variant where we reverse the runs of zeros.

%Y Cf. A000120, A005811.

%K nonn,base,look

%O 0,3

%A _Rémy Sigrist_, Dec 09 2018