login
Difference between 4^n and the nearest triangular number.
3

%I #26 Jun 16 2022 10:25:06

%S 0,1,1,-2,3,-11,1,-87,-167,-306,-500,-552,688,-3041,-579,20854,37075,

%T 55618,37108,-222296,-147729,891994,602155,-3523022,-2228805,14811346,

%U 11792251,-47737262,-1136517,375078994,741065851,1445763154,2746052116,4910207464,7492827856

%N Difference between 4^n and the nearest triangular number.

%H Harvey P. Dale, <a href="/A238455/b238455.txt">Table of n, a(n) for n = 0..1000</a>

%F a(n) = (1/2)*(-t^2 - t + 2*4^n), where t = floor(sqrt(2*4^n)) after formula in A053616. - _Michel Marcus_, Jun 16 2022

%e a(0) = 1 - 1 = 0.

%e a(1) = 4 - 3 = 1.

%e a(2) = 16 - 15 = 1.

%e a(3) = 64 - 66 = -2.

%e a(4) = 256 - 253 = 3.

%t db4n[n_]:=Module[{c=4^n,tr,t1,t2,d1,d2},tr=Floor[(Sqrt[8c+1]-1)/2];t1= (tr (tr+1))/ 2;t2=((tr+1)(tr+2))/2;d1=c-t1;d2=c-t2;If[d1<Abs[ d2], d1,d2]]; Array[ db4n,40,0] (* _Harvey P. Dale_, Jul 02 2019 *)

%o (Python)

%o def isqrt(a):

%o sr = 1 << (int.bit_length(int(a)) >> 1)

%o while a < sr*sr: sr>>=1

%o b = sr>>1

%o while b:

%o s = sr + b

%o if a >= s*s: sr = s

%o b>>=1

%o return sr

%o for n in range(77):

%o nn = 4**n

%o s = isqrt(2*nn)

%o if s*(s+1)//2 > nn: s-=1

%o d1 = nn - s*(s+1)//2

%o d2 = (s+1)*(s+2)//2 - nn

%o if d2 < d1: d1 = -d2

%o print(str(d1), end=',')

%o (PARI) a(n) = my(p=4^n, t=sqrtint(2*p)); (-t^2 - t + 2*p)/2; \\ _Michel Marcus_, Jun 16 2022

%Y Absolute values give the other bisection of A233327.

%Y Cf. A000079, A000217, A053616.

%K sign

%O 0,4

%A _Alex Ratushnyak_, Feb 26 2014