%I #44 Jul 28 2023 23:51:42
%S 3,1,0,5,2,-1,1,3,4,-1,-1,2,-1,1,0,7,9,-1,-1,4,2,-1,1,0,-1,-1,-1,3,-1,
%T -1,-1,5,8,1,0,6,-1,-1,-1,-1,7,-1,-1,-1,2,-1,1,4,5,-1,-1,-1,-1,-1,-1,
%U 3,6,-1,-1,2,-1,1,0,9,10,-1,-1,11,-1,-1,-1,-1,3,-1,-1,-1,2,-1,1,6,-1,-1,-1,4,-1
%N a(n) is the largest integer x such that n + 2^x is a square, or -1 if no such number exists.
%C a(n) is the maximum integer solution x of the indefinite equation n + 2^x = r^2 where n is a constant and r is a positive integer, or -1 if there are no solutions.
%C See A247763 for the number of solutions and for further information, references and links about this problem.
%C If n == 0 (mod 4), we first try x = 0 or 1; when x >= 2, the result can be derived from the result for n/4 (see formula).
%C If n == 2 (mod 4), the only possible values of x is 1, as otherwise n + 2^x == 2 (mod 4), so nonsquare.
%C If n == 3 (mod 4), the only possible values of x are 0 and 1, as otherwise n + 2^x == 3 (mod 4), so nonsquare.
%C If n == 1 (mod 4), or n = 4*k + 1 (k >= 0): we suggest that r = 2*m + 1, 4*k + 1 + 2^x = (2*m + 1)^2, thus k + 2^(x - 2) = m*(m + 1); if k is odd, the only possible values of x is 2 because m*(m + 1) is even.
%C If n = k^2 (k >= 1), 2^x = (r + k)*(r - k), so 2k must be in the form 2^i - 2^j.
%C The problem can be solved via reduction to three Mordell curves: n + z^3 = y^2, n + 2z^3 = y^2 or equivalently 4n + (2z)^3 = (2y)^2, n + 4z^3 = y^2 or equivalently 16n + (4z)^3 = (4y)^2, where z := 2^floor(x/3). For a given n, each of these three curves is known to have only a finite number of integer points (y,z), proving that x cannot be unbounded. - _Max Alekseyev_, Apr 26 2023
%H Thomas Scheuerle, <a href="/A362564/b362564.txt">Table of n, a(n) for n = 1..1000</a>
%F a(4*k + 2) = 1 if k + 1 is a square, or -1 otherwise.
%F a(4*k + 3) = 1 if 4*k + 5 is a square, or 0 is k + 1 is a square and 4*k + 5 is a nonsquare, or -1 otherwise.
%F a(4*k + 4) = a(k) + 2 if a(k) >= 0, or 0 if 4*k + 1 is a square and a(k) = -1, or -1 otherwise.
%F a(8*k + 5) = 2 if 8*k + 9 is a square, or -1 otherwise.
%F a((2^i - 2^j)^2) = i + j + 2 for i,j >= 0.
%F a(n) > -1 if A247763(n) > 0, or equivalently n is in A051204. - _Thomas Scheuerle_, May 02 2023
%e See COMMENTS section for further proof.
%e For n = 1, 1 + 2^3 = 9 = 3^2;
%e for n = 4, 4 + 2^5 = 36 = 6^2;
%e for n = 7, 7 + 2^1 = 9 = 3^2;
%e for n = 9, 9 + 2^4 = 25 = 5^2.
%o (Sage) def a362564(n): return max((3*v-2*k for k in range(3) for z,_,_ in EllipticCurve([0,4^k*n]).integral_points() if z>=1<<k and z==1<<(v:=valuation(z,2))), default=-1) # _Max Alekseyev_, Apr 26 2023
%Y Cf. A000079, A000290, A051204, A234000, A238454, A247763.
%K sign
%O 1,1
%A _Yifan Xie_, Apr 24 2023