OFFSET
1,2
COMMENTS
Also numbers where in the binary expansion the bit 0 or 1 occurs exactly once.
LINKS
David A. Corneth, Table of n, a(n) for n = 1..10011
EXAMPLE
4 = 2^2 is term as it is a power of 2.
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.
MATHEMATICA
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 *)
A384021list[11] (* Paolo Xausa, Jun 12 2025 *)
PROG
(PARI) upto(n) = {
my(res = List());
for(i = 0, logint(n, 2)+1,
pow2 = 1<<i;
listput(res, pow2);
for(j = 0, i-2,
listput(res, pow2 - 1<<j - 1);
);
);
Set(res)}
(Python)
from itertools import count, islice
def agen(): # generator of terms
yield from (1, 2)
for d in count(3):
m, b1 = 1<<(d-1), (1<<d) - 1
yield m
yield from (b1-(m>>i) for i in range(1, d))
print(list(islice(agen(), 58))) # Michael S. Branicky, May 18 2025
(Python)
def A384021(n):
def bisection(f, kmin=0, kmax=1):
while f(kmax) > kmax: kmax <<= 1
kmin = kmax >> 1
while kmax-kmin > 1:
kmid = kmax+kmin>>1
if f(kmid) <= kmid:
kmax = kmid
else:
kmin = kmid
return kmax
def f(x):
if x<=1: return n
l, s = x.bit_length(), bin(x)[2:]
if (m:=s.count('0'))>0: return n+x-s.index('0')+(m>1)-(l*(l-1)>>1)
return n+x+1-(l*(l+1)>>1)
return bisection(f, n, n) # Chai Wah Wu, May 21 2025
CROSSREFS
KEYWORD
nonn,base,easy
AUTHOR
David A. Corneth, May 17 2025
STATUS
approved
