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.

This page as a plain text file.
%I A246591 #30 Jan 03 2025 18:24:45
%S A246591 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,
%T A246591 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,
%U A246591 31,25,27,27,31,29,31,31,63,1,3,3,7,5,7,7,15,9,11,11
%N A246591 Smallest number that can be obtained by swapping 2 bits in the binary expansion of n.
%C A246591 Swap the first 1 with the last 0 in the binary expansion of n.
%H A246591 Alois P. Heinz, <a href="/A246591/b246591.txt">Table of n, a(n) for n = 0..8190</a>
%e A246591 If n = 12 = 1100_2 then a(12) = 0101_2 = 5.
%o A246591 (Python)
%o A246591 from itertools import combinations
%o A246591 def A246591(n):
%o A246591     if n <= 1:
%o A246591         return n
%o A246591     else:
%o A246591         s = bin(n)[2:]
%o A246591         l = len(s)
%o A246591         y = 2**l-1
%o A246591         for i in combinations(range(l), 2):
%o A246591             s2 = int(s[:i[0]]+s[i[1]]+s[i[0]+1:i[1]]+s[i[0]]+s[i[1]+1:], 2)
%o A246591             if s2 < y:
%o A246591                 y = s2
%o A246591         return y
%o A246591 # _Chai Wah Wu_, Sep 05 2014
%o A246591 (Python)
%o A246591 def A246591(n):
%o A246591     s = bin(n)[2:]
%o A246591     s2 = s.rstrip('1')
%o A246591     return(int(s2[1:-1]+'1'+s[len(s2):], 2) if (len(s2) > 0 and n > 1) else n)
%o A246591 # _Chai Wah Wu_, Sep 08 2014
%Y A246591 Cf. A241816, A246592, A246593, A246594.
%K A246591 nonn,base
%O A246591 0,4
%A A246591 _N. J. A. Sloane_, Sep 03 2014
%E A246591 More terms from _Alois P. Heinz_, Sep 03 2014