OFFSET
0,3
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers.
This sequence has similarities with A054429 (where we complement the bits to the right of the leftmost one digit).
LINKS
Rémy Sigrist, Table of n, a(n) for n = 0..8191
Rémy Sigrist, Colored scatterplot of the sequence for n = 0..2^13-1 (where the color denotes the position of the leftmost zero digit in the binary expansion of n)
FORMULA
a(n) = n iff n = 0 or n belongs to A075427.
a(n) = XOR(n,2^(A063250(n)-1)-1) if n>0 and A063250(n)>0. Otherwise a(n) = n. - Chai Wah Wu, Mar 09 2025
EXAMPLE
The first terms, in decimal and in binary, are:
n a(n) bin(n) bin(a(n))
-- ---- ------ ---------
0 0 0 0
1 1 1 1
2 2 10 10
3 3 11 11
4 5 100 101
5 4 101 100
6 6 110 110
7 7 111 111
8 11 1000 1011
9 10 1001 1010
10 9 1010 1001
11 8 1011 1000
12 13 1100 1101
13 12 1101 1100
14 14 1110 1110
15 15 1111 1111
16 23 10000 10111
PROG
(PARI) a(n) = { my (b = binary(n)); for (i = 1, #b, if (b[i]==0, for (j = i+1, #b, b[j] = 1-b[j]; ); return (fromdigits(b, 2)); ); ); return (n); }
(Python)
def a(n):
b = bin(n)[2:]
zi = b.find('0')
return n if zi == -1 else int(b[:zi+1]+"".join('0' if bi == '1' else '1' for bi in b[zi+1:]), 2)
print([a(n) for n in range(70)]) # Michael S. Branicky, Mar 09 2025
(Python)
def A381852(n): return n^((1<<n.bit_length()-t-1)-1) if n and (t:=bin(n)[2:].find('0'))!=-1 else n # Chai Wah Wu, Mar 09 2025
CROSSREFS
KEYWORD
nonn,base,easy
AUTHOR
Rémy Sigrist, Mar 08 2025
STATUS
approved
