This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.
%I A384021 #43 Jun 13 2025 08:20:28 %S A384021 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 A384021 123,125,126,128,191,223,239,247,251,253,254,256,383,447,479,495,503, %U A384021 507,509,510,512,767,895,959,991,1007,1015,1019,1021,1022,1024,1535,1791,1919 %N A384021 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 A384021 Also numbers where in the binary expansion the bit 0 or 1 occurs exactly once. %C A384021 Union of A000079 and A164874. - _Chai Wah Wu_, May 21 2025 %H A384021 David A. Corneth, <a href="/A384021/b384021.txt">Table of n, a(n) for n = 1..10011</a> %e A384021 4 = 2^2 is term as it is a power of 2. %e A384021 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 A384021 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 A384021 A384021list[11] (* _Paolo Xausa_, Jun 12 2025 *) %o A384021 (PARI) upto(n) = { %o A384021 my(res = List()); %o A384021 for(i = 0, logint(n, 2)+1, %o A384021 pow2 = 1<<i; %o A384021 listput(res, pow2); %o A384021 for(j = 0, i-2, %o A384021 listput(res, pow2 - 1<<j - 1); %o A384021 ); %o A384021 ); %o A384021 Set(res)} %o A384021 (Python) %o A384021 from itertools import count, islice %o A384021 def agen(): # generator of terms %o A384021 yield from (1, 2) %o A384021 for d in count(3): %o A384021 m, b1 = 1<<(d-1), (1<<d) - 1 %o A384021 yield m %o A384021 yield from (b1-(m>>i) for i in range(1, d)) %o A384021 print(list(islice(agen(), 58))) # _Michael S. Branicky_, May 18 2025 %o A384021 (Python) %o A384021 def A384021(n): %o A384021 def bisection(f,kmin=0,kmax=1): %o A384021 while f(kmax) > kmax: kmax <<= 1 %o A384021 kmin = kmax >> 1 %o A384021 while kmax-kmin > 1: %o A384021 kmid = kmax+kmin>>1 %o A384021 if f(kmid) <= kmid: %o A384021 kmax = kmid %o A384021 else: %o A384021 kmin = kmid %o A384021 return kmax %o A384021 def f(x): %o A384021 if x<=1: return n %o A384021 l, s = x.bit_length(), bin(x)[2:] %o A384021 if (m:=s.count('0'))>0: return n+x-s.index('0')+(m>1)-(l*(l-1)>>1) %o A384021 return n+x+1-(l*(l+1)>>1) %o A384021 return bisection(f,n,n) # _Chai Wah Wu_, May 21 2025 %Y A384021 Cf. A383666 (complement), A000079, A030130, A164874. %K A384021 nonn,base,easy %O A384021 1,2 %A A384021 _David A. Corneth_, May 17 2025