login

Year-end appeal: Please make a donation to the OEIS Foundation to support ongoing development and maintenance of the OEIS. We are now in our 61st year, we have over 378,000 sequences, and we’ve reached 11,000 citations (which often say “discovered thanks to the OEIS”).

a(n)=n for n <= 2; for n >= 3, a(n) = largest number that can be obtained by swapping two adjacent bits in the binary expansion of n.
5

%I #25 Sep 07 2014 08:05:38

%S 0,1,2,3,4,6,6,7,8,10,12,13,12,14,14,15,16,18,20,21,24,25,26,27,24,26,

%T 28,29,28,30,30,31,32,34,36,37,40,41,42,43,48,49,50,51,52,53,54,55,48,

%U 50,52,53,56,57,58,59,56,58,60,61,60,62,62,63,64,66,68

%N a(n)=n for n <= 2; for n >= 3, a(n) = largest number that can be obtained by swapping two adjacent bits in the binary expansion of n.

%C In both this sequence and A246593 you are not allowed to touch any of the invisible 0's before the leading 1.

%C Scanning from the left, find first occurrence of '01' in binary expansion and replace with '10' and return decimal representation or return n if no such swap exists. - _Chai Wah Wu_, Sep 06 2014

%H Alois P. Heinz, <a href="/A246594/b246594.txt">Table of n, a(n) for n = 0..8192</a>

%e If n = 17 = 10001_2 then a(17) = 10010_2 = 18.

%o (Python)

%o def A246594(n):

%o ....s = bin(n)[2:]

%o ....for i in range(len(s)-1):

%o ........if s[i:i+2] == '01':

%o ............return int(s[:i]+'10'+s[i+2:],2)

%o ....else:

%o ........return n # _Chai Wah Wu_, Sep 06 2014

%Y Cf. A241816, A246591, A246592, A246593.

%K nonn,base

%O 0,3

%A _N. J. A. Sloane_, Sep 03 2014

%E Corrected definition and more terms from _Alois P. Heinz_, Sep 04 2014

%E Corrected typo in definition - _Chai Wah Wu_, Sep 06 2014