OFFSET
0,5
COMMENTS
In binary 0, 0, 1, 1, 10, 11, 11, 101, 101, 110, 111, 111, 1001, 1010, 1011, 1101, 1110, 1111, 1111, 10010, 10011, 10011, 10101, ...
a(n) = m is the smallest solution to m + bitcount(m) = n or n-1. So a(n) = smaller nonzero of A228086(n) and A228086(n-1) (for n>=2). - Kevin Ryde, Jul 05 2020
LINKS
Kevin Ryde, PARI/GP code and explanation, quantity "b(n)".
FORMULA
a(n) + bitcount(a(n)) + A334820(n) = n for n>=0.
MAPLE
s:= proc(n) option remember; `if`(n=0, [0, 0][], (l->
[l[], map(x-> x+2^(n-1), l[n..-1])[]][])([s(n-1)]))
end:
s(7); # gives 136 = A005126(7) terms; # Alois P. Heinz, Jul 04 2020
MATHEMATICA
a[n_] := If[n == 0, 0,
Module[{m = n, k = Floor@Log2[n]}, m -= k + 1; While[k >= 0,
If[BitGet[m, k] == 0, m++;
If[BitGet[m, k] == 1, Return[m-1]]]; k--]; m]];
Table[a[n], {n, 0, 67}] (* Jean-François Alcover, May 30 2022, after Kevin Ryde *)
PROG
(PARI) a(n) = { if(n, my(k=logint(n, 2)); n-=k+1;
while(k>=0, if(!bittest(n, k), n++; if(bittest(n, k), return(n-1))); k--));
n; } \\ Kevin Ryde, Jul 05 2020
CROSSREFS
KEYWORD
nonn
AUTHOR
Richard Aime Blavy, Jun 15 2020
STATUS
approved