A073138 Largest number having in its binary representation the same number of 0's and 1's as n.
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, 28, 30, 28, 30, 30, 31, 32, 48, 48, 56, 48, 56, 56, 60, 48, 56, 56, 60, 56, 60, 60, 62, 48, 56, 56, 60, 56, 60, 60, 62, 56, 60, 60, 62, 60, 62, 62, 63, 64, 96, 96, 112, 96, 112, 112
Offset: 0
Examples
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'.
Links
- Seiichi Manyama, Table of n, a(n) for n = 0..8191 (terms 0..1023 from T. D. Noe)
- Index entries for sequences related to binary expansion of n
Crossrefs
Programs
-
Haskell
a073138 n = a038573 n * a080100 n -- Reinhard Zumkeller, Jan 16 2012
-
Maple
a:= n-> Bits[Join](sort(Bits[Split](n))): seq(a(n), n=0..100); # Alois P. Heinz, Jun 26 2021
-
Mathematica
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}] 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}] 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 *) Table[FromDigits[ReverseSort[IntegerDigits[n,2]],2],{n,0,70}] (* Harvey P. Dale, Mar 13 2023 *)
-
PARI
a(n) = fromdigits(vecsort(binary(n),,4), 2); \\ Michel Marcus, Sep 26 2018
-
Python
def a(n): return int("".join(sorted(bin(n)[2:], reverse=True)), 2) print([a(n) for n in range(71)]) # Michael S. Branicky, Jun 27 2021
-
Python
def A073138(n): return (m:=1<
>n.bit_count()) # Chai Wah Wu, Aug 18 2025
Formula
a(n+1) = a(floor(n/2))*2 + (n mod 2)*(2^floor(log_2(n)) - a(floor(n/2))); a(0)=0.
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
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
n <= a(n) < 2n. - Charles R Greathouse IV, Aug 07 2024
Comments