login
A246591
Smallest number that can be obtained by swapping 2 bits in the binary expansion of n.
5
0, 1, 1, 3, 1, 3, 3, 7, 1, 3, 3, 7, 5, 7, 7, 15, 1, 3, 3, 7, 5, 7, 7, 15, 9, 11, 11, 15, 13, 15, 15, 31, 1, 3, 3, 7, 5, 7, 7, 15, 9, 11, 11, 15, 13, 15, 15, 31, 17, 19, 19, 23, 21, 23, 23, 31, 25, 27, 27, 31, 29, 31, 31, 63, 1, 3, 3, 7, 5, 7, 7, 15, 9, 11, 11
OFFSET
0,4
COMMENTS
Swap the first 1 with the last 0 in the binary expansion of n.
LINKS
EXAMPLE
If n = 12 = 1100_2 then a(12) = 0101_2 = 5.
PROG
(Python)
from itertools import combinations
def A246591(n):
....if n <= 1:
........return n
....else:
........s = bin(n)[2:]
........l = len(s)
........y = 2**l-1
........for i in combinations(range(l), 2):
............s2 = int(s[:i[0]]+s[i[1]]+s[i[0]+1:i[1]]+s[i[0]]+s[i[1]+1:], 2)
............if s2 < y:
................y = s2
........return y
# Chai Wah Wu, Sep 05 2014
(Python)
def A246591(n):
....s = bin(n)[2:]
....s2 = s.rstrip('1')
....return(int(s2[1:-1]+'1'+s[len(s2):], 2) if (len(s2) > 0 and n > 1) else n)
# Chai Wah Wu, Sep 08 2014
CROSSREFS
KEYWORD
nonn,base
AUTHOR
N. J. A. Sloane, Sep 03 2014
EXTENSIONS
More terms from Alois P. Heinz, Sep 03 2014
STATUS
approved