%I #32 May 30 2022 08:11:00
%S 0,0,1,1,2,3,3,5,5,6,7,7,9,10,11,11,13,13,14,15,15,18,19,19,21,21,22,
%T 23,23,25,26,27,27,29,29,30,31,31,35,35,37,37,38,39,39,41,42,43,43,45,
%U 45,46,47,47,50,51,51,53,53,54,55,55,57,58,59,59,61,61
%N Sequence is limit_{k->oo} s_k, where s_k = s_{k-1}, s_{k-1}[k-1] + 2^(k-1), ..., s_{k-1}[end] + 2^(k-1) starting with s_0 = s_0[0..1] = 0,0.
%C 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, ...
%C 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
%H Kevin Ryde, <a href="http://user42.tuxfamily.org/seq-A334820/index.html">PARI/GP code and explanation</a>, quantity "b(n)".
%F a(n) + bitcount(a(n)) + A334820(n) = n for n>=0.
%p s:= proc(n) option remember; `if`(n=0, [0, 0][], (l->
%p [l[], map(x-> x+2^(n-1), l[n..-1])[]][])([s(n-1)]))
%p end:
%p s(7); # gives 136 = A005126(7) terms; # _Alois P. Heinz_, Jul 04 2020
%t a[n_] := If[n == 0, 0,
%t Module[{m = n, k = Floor@Log2[n]}, m -= k + 1; While[k >= 0,
%t If[BitGet[m, k] == 0, m++;
%t If[BitGet[m, k] == 1, Return[m-1]]]; k--]; m]];
%t Table[a[n], {n, 0, 67}] (* _Jean-François Alcover_, May 30 2022, after _Kevin Ryde_ *)
%o (PARI) a(n) = { if(n, my(k=logint(n,2)); n-=k+1;
%o while(k>=0, if(!bittest(n,k), n++; if(bittest(n,k), return(n-1))); k--));
%o n; } \\ _Kevin Ryde_, Jul 05 2020
%Y Cf. A005126, A334820.
%K nonn
%O 0,5
%A _Richard Aime Blavy_, Jun 15 2020