Diophantine equation $3^n-1=2x^2$

A standard (although possibly not the most efficient) way to solve equations of this sort is to find all integer points on each of the elliptic curves $$ E_1: X^3-1=2Y^2,\quad E_2:3X^3-1=2Y^2,\quad E_3:9X^3-1=2Y^2. $$ Next pick out the solutions that have $X$ equal to a power of $3$. These then give the solutions to your equation with $3^n=X$ and $x=Y$. Finding all integer solutions on elliptic curves with small coefficients is, these days, quite standard and is even built into many computer algebra systems.


This problem happens to have appeared on the Polish Mathematical Olympiad camp in 2015. Here is the official solution of the problem: (I use $m$ in place of $x$ because this is how the problem was stated there)

Suppose first $n$ is even, say $n=2k$. Then the equation is equivalent to $(3^k+1)(3^k-1)=2m^2$. Clearly $\gcd(3^k+1,3^k-1)=2$, so one of these numbers must be a perfect square (and the other one twice a perfect square). $3^k-1\equiv 2\pmod 3$, so it can't be a square, hence $3^k+1=t^2,3^k=(t-1)(t+1)$. Therefore, $t-1,t+1$ are powers of $3$. But they can't both be divisible by $3$, so $t-1=1,t=2$. This leads to solution $(m,n)=(2,2)$.

Now suppose $n$ is odd, say $n=2k+1$. Letting $t=3^k$ we get $3t^2-2m^2=1$. Setting $t=2v+u,m=3v+u$ (check $u,v$ are integers) we get the Pell's equation $u^2-6v^2=1$. Standard theory gives us that all its solutions are generated as follows: $$(u_0,v_0)=(5,2),(u_{i+1},v_{i+1})=(5u_i+12v_i,2u_i+5v_i).$$ From this we can get a recurrence for $t=t_i=2v_i+u_i$: $$t_{-1}=1,t_0=9,t_{i+2}=10t_{i+1}-t_i$$ Looking modulo $27$ and $17$, we find $$t_i\equiv 0\pmod{27}\Leftrightarrow i\equiv 3\pmod 9\Leftrightarrow t_i\equiv 0\pmod{17}$$ It follows that the only powers of $3$ in the sequence $t_i$ are $1,9$ which correspond to solutions $(1,1),(5,11)$.

So all the solutions are $(1,1),(2,2),(5,11)$.

Edit: This is a solution for positive integers. If we allow nonpositive integers, we also get $(0,0)$ and $(1,-1),(2,-2),(5,-11)$, but rather clearly no more.


This Diophantine equation arises naturally in coding theory, because $2x^2+1$ is the number of points in a ball of radius $2$ in the ternary Hamming space $\{0,1,2\}^x$. It is known that $3^5-1 = 2\cdot 11^2$ (which corresponds to the ternary Golay code) is the last solution.

A proof, using factorization in ${\bf Z}[\sqrt{-2}]$ (as suggested by Geoff Robinson) followed by Skolem's $p$-adic method, is given in the paper

Jungmin Ahn, Hyun Kwang Kim, Jung Soo Kim, Mina Kim: Classification of perfect linear codes with crown poset structure, Discrete Math. 268 (2003), 21-30.