cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A246591 Smallest number that can be obtained by swapping 2 bits in the binary expansion of n.

Original entry on oeis.org

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

Views

Author

N. J. A. Sloane, Sep 03 2014

Keywords

Comments

Swap the first 1 with the last 0 in the binary expansion of n.

Examples

			If n = 12 = 1100_2 then a(12) = 0101_2 = 5.
		

Crossrefs

Programs

  • 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

Extensions

More terms from Alois P. Heinz, Sep 03 2014