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.

A246594 a(n)=n for n <= 2; for n >= 3, a(n) = largest number that can be obtained by swapping two adjacent bits in the binary expansion of n.

Original entry on oeis.org

0, 1, 2, 3, 4, 6, 6, 7, 8, 10, 12, 13, 12, 14, 14, 15, 16, 18, 20, 21, 24, 25, 26, 27, 24, 26, 28, 29, 28, 30, 30, 31, 32, 34, 36, 37, 40, 41, 42, 43, 48, 49, 50, 51, 52, 53, 54, 55, 48, 50, 52, 53, 56, 57, 58, 59, 56, 58, 60, 61, 60, 62, 62, 63, 64, 66, 68
Offset: 0

Views

Author

N. J. A. Sloane, Sep 03 2014

Keywords

Comments

In both this sequence and A246593 you are not allowed to touch any of the invisible 0's before the leading 1.
Scanning from the left, find first occurrence of '01' in binary expansion and replace with '10' and return decimal representation or return n if no such swap exists. - Chai Wah Wu, Sep 06 2014

Examples

			If n = 17 = 10001_2 then a(17) = 10010_2 = 18.
		

Crossrefs

Programs

  • Mathematica
    A246594[n_] := FromDigits[StringReplace[IntegerString[n, 2], "01" -> "10", 1], 2];
    Array[A246594, 100, 0] (* Paolo Xausa, Mar 07 2025 *)
  • Python
    def A246594(n):
        s = bin(n)[2:]
        for i in range(len(s)-1):
            if s[i:i+2] == '01':
                return int(s[:i]+'10'+s[i+2:], 2)
        else:
            return n # Chai Wah Wu, Sep 06 2014

Extensions

Corrected definition and more terms from Alois P. Heinz, Sep 04 2014
Corrected typo in definition - Chai Wah Wu, Sep 06 2014