A166404 Self-inverse permutation of natural numbers obtained by exchanging the run lengths of adjacent runs of ones and zeros in the binary expansion of n, starting from the most significant run of 1's. (See the example lines).
0, 1, 2, 3, 6, 5, 4, 7, 14, 13, 10, 11, 12, 9, 8, 15, 30, 29, 26, 27, 22, 21, 20, 23, 28, 25, 18, 19, 24, 17, 16, 31, 62, 61, 58, 59, 54, 53, 52, 55, 46, 45, 42, 43, 44, 41, 40, 47, 60, 57, 50, 51, 38, 37, 36, 39, 56, 49, 34, 35, 48, 33, 32, 63, 126, 125, 122, 123, 118, 117
Offset: 0
Examples
68 in binary is 1000100, so it consists of runs of 1, 3, 1 and 2 bits of the same value (alternatively 1 or 0) counted from the most significant end. Swapping these (the first with the second, the third with the fourth, etc.) gives the run lengths of [3,1,2,1], and the reconstructed binary string looks now as 1110110, which is 118 in binary. Thus a(68)=118. Likewise, 67 in binary is 1000011, so it consists of runs of 1, 4 and 2 counted from the most significant end. Now we can swap just the first and second of these runs, with the remaining third run staying as it is, so we get run lengths of [4,1,2], and the reconstructed binary string looks now as 1111011, which is 123 in binary. Thus a(67)=123.
Links
Programs
-
Python
def a(n): if n==0: return 0 x=bin(n)[2:] r=[] s=1 p="" for i in range(1, len(x)): if x[i - 1]==x[i]: s+=1 else: r+=[s, ] s=1 l=r+[s] L=[] if len(l)%2==0: for i in range(0, len(l), 2): L+=[l[i + 1], l[i]] else: b=l[-1] for i in range(0, len(l) - 1, 2): L+=[l[i + 1], l[i]] L+=[b, ] for i in range(len(L)): if i%2==0: p+='1'*L[i] else: p+='0'*L[i] return int(p, 2) print([a(n) for n in range(101)]) # Indranil Ghosh, Jun 12 2017