OFFSET
0,3
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers with infinitely many fixed points (A345362).
LINKS
FORMULA
EXAMPLE
For n = 43:
- the binary expansion of n is "101011",
- it has 6 binary digits, so we pad it with 2 leading 0's,
- the first half is "0010" and its reversal is "0100",
- the second half is "1011" and its reversal is "1101",
- so the binary expansion of a(43) is "01001101",
- and a(43) = 77.
PROG
(PARI) a(n) = { my (b=binary(n), x); for (k=1, oo, x=2^k-#b; if (x>=0, b=concat(vector(x), b); return (fromdigits(concat(Vecrev(b[1..#b/2]), Vecrev(b[#b/2+1..#b])), 2)))) }
(Python)
def a(n):
b = bin(n)[2:]
bb = bin(len(b))[2:]
if bb != '1' + '0'*(len(bb)-1): b = '0'*(2**len(bb) - len(b)) + b
return int(b[:len(b)//2][::-1] + b[len(b)//2:][::-1], 2)
print([a(n) for n in range(60)]) # Michael S. Branicky, Jun 15 2021
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Rémy Sigrist, Jun 15 2021
STATUS
approved