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.

A073138 Largest number having in its binary representation the same number of 0's and 1's as n.

This page as a plain text file.
%I A073138 #51 Aug 18 2025 20:34:53
%S A073138 0,1,2,3,4,6,6,7,8,12,12,14,12,14,14,15,16,24,24,28,24,28,28,30,24,28,
%T A073138 28,30,28,30,30,31,32,48,48,56,48,56,56,60,48,56,56,60,56,60,60,62,48,
%U A073138 56,56,60,56,60,60,62,56,60,60,62,60,62,62,63,64,96,96,112,96,112,112
%N A073138 Largest number having in its binary representation the same number of 0's and 1's as n.
%C A073138 From Trevor Green (green(AT)snoopy.usask.ca), Nov 26 2003: (Start)
%C A073138 a(n)/n has an accumulation point at x exactly when x is in the interval [1, 2]. Proof: Clearly n <= a(n) < 2n. Let b(n) = a(n)/n, then b(n) must always lie in [1,2) and all the accumulation points of the sequence must lie in [1,2]. We shall show that every such number is an accumulation point.
%C A073138 First, consider any d-bit integer n. Suppose that z of these bits are 0. Let n' be the (d+z)-bit integer whose first d bits are the same as those of n and whose remaining bits are all 1. Then a(n') will have to be the (d+z)-bit integer whose first d bits are all 1 and whose last z bits are all 0.
%C A073138 Thus n' = (n+1)*2^z-1; a(n') = (2^d-1)2^z; and b(n') = (2^d-1)/(n+1) + epsilon, where 0 < epsilon < 2^(1-d). So to get an accumulation point x, we just choose n(d) to be the d-bit integer such that (2^d-1)/(n(d)+1) < x <= (2^d-1)/n(d), or equivalently, n(d) = floor((2^d-1)/x). If x lies in [1,2), then n(d) will always be a d-bit number for sufficiently large d.
%C A073138 Then n'(d) yields an increasing subsequence of the integers for which b(n'(d)) converges to x. For x = 2, choose n(d) = 2^(d-1), which is always a d-bit number; then b(n'(d)) = (2^d-1)/(2^(d-1)+1) + epsilon = 2 + epsilon', where epsilon' also heads for 0 as d blows up. This proves the claim.
%C A073138 (End)
%H A073138 Seiichi Manyama, <a href="/A073138/b073138.txt">Table of n, a(n) for n = 0..8191</a> (terms 0..1023 from T. D. Noe)
%H A073138 <a href="/index/Bi#binary">Index entries for sequences related to binary expansion of n</a>
%F A073138 a(n+1) = a(floor(n/2))*2 + (n mod 2)*(2^floor(log_2(n)) - a(floor(n/2))); a(0)=0.
%F A073138 A023416(a(n)) = A023416(n), A000120(a(n)) = A000120(n).
%F A073138 a(0)=0, a(1)=1, a(2n) = 2a(n), a(2n+1) = a(n) + 2^floor(log_2(n)). - _Ralf Stephan_, Oct 05 2003
%F A073138 a(n) = 2^(floor(log_2(n)) + 1) * (1 - 2^(-d(n))) where d(n) = digit sum of base-2 expansion of n. - Trevor G. Hyde (thyde12(AT)amherst.edu), Jul 14 2008
%F A073138 a(n) = A038573(n) * A080100(n). - _Reinhard Zumkeller_, Jan 16 2012
%F A073138 n <= a(n) < 2n. - _Charles R Greathouse IV_, Aug 07 2024
%e A073138 a(20)=24, as 20='10100' and 24 is the greatest number having two 1's and three 0's: 17='10001', 18='10010', 20='10100' and 24='11000'.
%p A073138 a:= n-> Bits[Join](sort(Bits[Split](n))):
%p A073138 seq(a(n), n=0..100);  # _Alois P. Heinz_, Jun 26 2021
%t A073138 f[n_] := Module[{idn=IntegerDigits[n, 2], o, l}, l=Length[idn]; o=Count[idn, 1]; FromDigits[Join[Table[1, {o}], Table[0, {l-o}]], 2]]; Table[f[i], {i, 0, 70}]
%t A073138 ln[n_] := Module[{idn=IntegerDigits[n, 2], len, zer}, len=Length[idn]; zer=Count[idn, 0]; FromDigits[Join[Table[1, {len-zer}], Table[0, {zer}]], 2]]; Table[ln[i], {i, 0, 70}]
%t A073138 a[z_] := 2^(Floor[Log[2, z]] + 1) * (1 - 2^(-Sum[k, {k, IntegerDigits[n, 2]}])) Column[Table[a[p], {p, 500}], Right] (* Trevor G. Hyde (thyde12(AT)amherst.edu), Jul 14 2008 *)
%t A073138 Table[FromDigits[ReverseSort[IntegerDigits[n,2]],2],{n,0,70}] (* _Harvey P. Dale_, Mar 13 2023 *)
%o A073138 (Haskell)
%o A073138 a073138 n = a038573 n * a080100 n  -- _Reinhard Zumkeller_, Jan 16 2012
%o A073138 (PARI) a(n) = fromdigits(vecsort(binary(n),,4), 2); \\ _Michel Marcus_, Sep 26 2018
%o A073138 (Python)
%o A073138 def a(n): return int("".join(sorted(bin(n)[2:], reverse=True)), 2)
%o A073138 print([a(n) for n in range(71)]) # _Michael S. Branicky_, Jun 27 2021
%o A073138 (Python)
%o A073138 def A073138(n): return (m:=1<<n.bit_length())-(m>>n.bit_count()) # _Chai Wah Wu_, Aug 18 2025
%Y A073138 Cf. A007088, A073137, A000523, A073139, A073140, A073141, A319650.
%Y A073138 Cf. A030109.
%Y A073138 Cf. A038573.
%Y A073138 Decimal equivalent of A221714. - _N. J. A. Sloane_, Jan 26 2013
%K A073138 nonn,nice,look,easy
%O A073138 0,3
%A A073138 _Reinhard Zumkeller_, Jul 16 2002