%I #46 Nov 22 2025 22:38:18
%S 1,2,4,5,6,8,11,13,14,16,23,27,29,30,32,47,55,59,61,62,64,95,111,119,
%T 123,125,126,128,191,223,239,247,251,253,254,256,383,447,479,495,503,
%U 507,509,510,512,767,895,959,991,1007,1015,1019,1021,1022,1024,1535,1791,1919
%N Powers of 2 along with numbers one power of 2 less than binary repunits, but the power of two subtracted does not flip the leading bit.
%C Also numbers where in the binary expansion the bit 0 or 1 occurs exactly once.
%C Union of A000079 and A164874. - _Chai Wah Wu_, May 21 2025
%H David A. Corneth, <a href="/A384021/b384021.txt">Table of n, a(n) for n = 1..10011</a>
%e 4 = 2^2 is term as it is a power of 2.
%e 5 is a term as 5 = (2^3 - 1) - 2^1; a power of two less than a binary repunit and subtracting 2 from 7 does not flip the most significant bit of 7.
%t A384021list[k_] := Flatten[{1, Table[{2^i - 1 - BitShiftRight[2^i, Range[2, i]], 2^i}, {i, 2 - Boole[k == 1], k}]}]; (* returns terms up to 2^k *)
%t A384021list[11] (* _Paolo Xausa_, Jun 12 2025 *)
%o (PARI) upto(n) = {
%o my(res = List());
%o for(i = 0, logint(n, 2)+1,
%o pow2 = 1<<i;
%o listput(res, pow2);
%o for(j = 0, i-2,
%o listput(res, pow2 - 1<<j - 1);
%o );
%o );
%o Set(res)}
%o (Python)
%o from itertools import count, islice
%o def agen(): # generator of terms
%o yield from (1, 2)
%o for d in count(3):
%o m, b1 = 1<<(d-1), (1<<d) - 1
%o yield m
%o yield from (b1-(m>>i) for i in range(1, d))
%o print(list(islice(agen(), 58))) # _Michael S. Branicky_, May 18 2025
%o (Python)
%o def A384021(n):
%o def bisection(f,kmin=0,kmax=1):
%o while f(kmax) > kmax: kmax <<= 1
%o kmin = kmax >> 1
%o while kmax-kmin > 1:
%o kmid = kmax+kmin>>1
%o if f(kmid) <= kmid:
%o kmax = kmid
%o else:
%o kmin = kmid
%o return kmax
%o def f(x):
%o if x<=1: return n
%o l, s = x.bit_length(), bin(x)[2:]
%o if (m:=s.count('0'))>0: return n+x-s.index('0')+(m>1)-(l*(l-1)>>1)
%o return n+x+1-(l*(l+1)>>1)
%o return bisection(f,n,n) # _Chai Wah Wu_, May 21 2025
%Y Cf. A383666 (complement), A000079, A030130, A164874.
%K nonn,base,easy
%O 1,2
%A _David A. Corneth_, May 17 2025