OFFSET
0,3
LINKS
FORMULA
a(0)=0, a(1)=1; for n > 1, let C = 2^(floor(log_2(n))-1) = A072376(n); then a(n) = a(n-C) + C if n < 3*C; otherwise a(n) = 2*a(n - 2*C) + 1. [corrected by Jon E. Schoenfield, Jun 27 2021]
For n > 0: a(n) = (2^(A000120(n) - 1)) * (2^A023416(n) + 1) - 1. - Corrected by Michel Marcus, Nov 15 2013
EXAMPLE
a(20)=17, as 20='10100' and 17 is the smallest number having two 1's and three 0's: 17='10001', 18='10010', 20='10100' and 24='11000'.
MAPLE
a:= n-> (l-> (2^nops(l)+2^add(i, i=l))/2-1)(Bits[Split](n)):
seq(a(n), n=0..100); # Alois P. Heinz, Jun 26 2021
MATHEMATICA
lnb[n_]:=Module[{sidn=Sort[IntegerDigits[n, 2]]}, FromDigits[Join[{1}, Most[ sidn]], 2]]; Join[{0}, Array[lnb, 80]] (* Harvey P. Dale, Aug 04 2014 *)
PROG
(Python)
def a(n):
b = bin(n)[2:]; z = b.count('0'); w = len(b) - z
return int('1'*(w > 0) + '0'*z + '1'*(w-1), 2)
print([a(n) for n in range(73)]) # Michael S. Branicky, Jun 26 2021
(Python)
def a(n): b = bin(n)[2:]; return int(b[0] + "".join(sorted(b[1:])), 2)
print([a(n) for n in range(73)]) # Michael S. Branicky, Jun 26 2021
(PARI) a(n) = if(n==0, 0, 1<<logint(n, 2) + 1<<(hammingweight(n)-1) - 1); \\ Kevin Ryde, Jun 26 2021
CROSSREFS
KEYWORD
nonn
AUTHOR
Reinhard Zumkeller, Jul 16 2002
STATUS
approved