Finding non-negative integers $m$ such that $(1 + \sqrt{-2})^m$ has real part $\pm 1$.

Here is an approach different from the one in the link of Will's answer: think $2$-adically. Since $(1+\sqrt{-2})^4 = 1 + (-8-4\sqrt{-2}) = 1 + \gamma$, with $|\gamma|_2 = 1/(4\sqrt{2}) < 1/2$, we can view $a_m$ as a $2$-adic analytic function by fixing $m \bmod 4$: picking $r \in \{0,1,2,3\}$ and writing $m = 4q+r$ where $q$ runs through the natural numbers, the sequence $f_r(q) = a_{4q+r}$ with $r$ fixed can be interpolated from nonnegative integers $q$ to $2$-adic integers $x$, giving a $2$-adic analytic function $f_r$ on $\mathbf Z_2$: $$ f_r(x) = \frac{(1+\sqrt{-2})^r(1+\gamma)^x + (1-\sqrt{-2})^r(1+\overline{\gamma})^x}{2}, $$ where $\overline{\gamma} = -8+4\sqrt{-2}$ is the conjugate of $\gamma$ (same $2$-adic absolute value). This is $2$-adic analytic in $x$ because in the $2$-adics we can write $c^x = \exp((\log c)x)$ when $|c - 1|_2 < 1/2$ and then expand the exponential function as a power series in $x$. You'd use $c = 1+\gamma$ and $c = 1+\overline{\gamma}$. Here $c \not\in \mathbf Q_2$, but in a finite extension $\mathbf Q_2(\sqrt{-2})$. The coefficients of $f_r(x)$ lie in the field $K = \mathbf Q_2(\sqrt{-2})$, which has ring of integers $\mathcal O_K = {\mathbf Z}_2[\sqrt{-2}]$.

To see how often $a_m = 1$ or $a_m = -1$, we can try to see more generally how often $f_r(x) = 1$ or $f_r(x) = -1$ (writing $m$ as $4x + r$ with $r$ fixed and $x$ running through $\mathbf Z_2$) and hope that the bound on the number of $2$-adic integer solutions $x$ to each equation is already accounted for by the number of known integer solutions of the original equations $a_m = 1$ and $a_m = -1$. Before we do that, however, I want to point out why $p$-adic analysis at least tells us something qualitative: for any integer $c$ the equation $a_m = c$ is satisfied for only finitely many $m$. Indeed, because a nonconstant $p$-adic analytic function on $\mathbf Z_p$ (or on any disc $\{|x|_p \leq r\}$ in any $p$-adic field) assumes any particular value only finitely many times (this is an analogue of a nonconstant holomorphic function on a disc in $\mathbf C$ taking on any particular value only finitely many times), the four equations $f_0(x) = c$, $f_1(x) = c$, $f_2(x) = c$, and $f_3(x) = c$ each have finitely many solutions $x$ in $\mathbf Z_2$, and thus the equation $a_m = c$ holds for finitely many integers $m \geq 0$. Hence in the real numbers, $|a_m| \rightarrow \infty$ as $m \rightarrow \infty$ because $|a_m|$ is an integer that has any value just finitely many times. So by using $2$-adic analysis we can make a conclusion about the behavior of $a_m$ as a real number. :)

The OP guessed from the data that $a_m = 1$ only when $m = 0, 1$, and $5$, and $a_m = -1$ only when $m = 2$. By writing $m = 4q+r$ and replacing $q$ with a $2$-adic integer variable $x$, the guess would be true if the only $2$-adic integer solution of $f_0(x) = 1$ is $x = 0$ (corr. to $a_0 = 1$), of $f_1(x) = 1$ is $x = 0$ and $x = 1$ (corr. to $a_1 = 1$ and $a_5 = 1$), of $f_2(x) = -1$ is $x = 0$ (corr. to $a_2 = -1$), and if the equations $f_0(x) = -1$, $f_1(x) = -1$, $f_2(x) = 1$, and $f_3(x) = \pm 1$ have no $2$-adic integer solutions.

There is a standard method to bound the number of times a nonconstant $p$-adic power series vanishes, called Strassman's theorem: if $f(x) = \sum_{n \geq 0} a_nx^n$ is a nonconstant power series with coefficients in a $p$-adic field and the coefficients tend to $0$ but are not all $0$, then the number of $p$-adic solutions to $f(x) = 0$ satisfying $|x|_p \leq 1$ is at most $N$, where $N$ is chosen as the position farthest out into the series where a coefficient of maximum absolute value occurs, i.e., $|a_N|_p = \max |a_n|_p$ and $N$ is as large as possible. (There is a maximal $N$ since the coefficients tend to $0$ and aren't all $0$.) We want to apply Strassman's theorem to the series $f(x) = f_r(x) - 1$ and $f(x) = f_r(x) + 1$ with coefficients in $K = \mathbf Q_2(\sqrt{-2})$.

In practice Strassman's bound often works out nicely, in the sense that the upper bound you get is the number of solutions you already know about, but strictly speaking there is no assurance that the $p$-adic power series couldn't vanish at a $p$-adic integer that is not an integer. If that happens the Strassman bound wouldn't let you know for sure that you've found all the integer solutions already. For instance, if you expect a $p$-adic power series vanishes at just one nonnegative integer and Strassman's bound is two, you'd need to rule out the possibility that a second zero in the $p$-adic integers is a nonnegative integer.

Fortunately for us, Strassman's bound for the particular problem we're looking at doesn't leave any room for unexpected solutions. For each $r \in \{0,1,2,3\}$, I have checked with Strassman's bound that the number of $2$-adic solutions $x$ to $f_r(x) - 1 = 0$ and $f_r(x) + 1 = 0$ with $|x|_2 \leq 1$ is already accounted for by the known integer occurrences of $a_m = 1$ and $a_m = -1$:

1) the largest (in the $2$-adic sense) coefficient of $f_0(x) - 1$ is only in the linear term, so $f_0(x) = 1$ has at most one $\mathbf Z_2$-solution.

2) the largest coefficient of $f_1(x) - 1$ occurs in the linear and quadratic terms, so the equation $f_1(x) = 1$ has at most two $\mathbf Z_2$-solutions.

3) the largest coefficient of $f_2(x) + 1$ is only in the linear term, so $f_2(x) = -1$ has at most one $\mathbf Z_2$-solution.

4) the largest coefficients of $f_0(x) + 1$, $f_1(x) + 1$, $f_2(x) - 1$, and $f_3(x) \pm 1$ are all only in the constant terms, so the equations $f_0(x) = -1$, $f_1(x) = -1$, $f_2(x) = 1$, and $f_3(x) = \pm 1$ have no $2$-adic integer solutions.

Computing the $2$-adic absolute values of the coefficients of these power series (well, getting decent upper bounds on them in general, and then computing exactly the $2$-adic absolute value of the first couple of coefficients) to apply Strassman's theorem requires several pages, so I have to omit the details. But it really does all work out!


EEDDIITT: you have all the solutions. Proved in Y. Bugeaud and T.N. Shorey, On the number of solutions of the generalized Ramanujan-Nagell equation, I. Jour. reine angew. Math. vol. 539 (2001) pages 55-74. Preprint is item number 92 at http://www.math.tifr.res.in/~shorey/

Statement found in N. Saradha and Anitha Srivanasan, Genaralized Lebesgue-Ramanujan-Nagell Equations, which I found online as a preprint, but has appeared in a book: Saradha, N.; Srinivasan, Anitha (2008). "Generalized Lebesgue–Ramanujan–Nagell equations". In Saradha, N. Diophantine Equations. Narosa. pp. 207–223.

ORIGINAL: Meanwhile, the governing Diophantine equation $$ 3^m - 1 = 2 y^2 $$ is of Ramanujan Nagell type and has finitely many solutions. Perhaps someone has worked this one out in its entirety.

Let's see, you are asking about when the entry $x = \pm 1$ in $$ \left( \begin{array}{rr} 1 & -2 \\ 1 & 1 \end{array} \right)^m \; \; = \; \; \left( \begin{array}{rr} x & -2y \\ y & x \end{array} \right), $$ with determinant $3^m = x^2 + 2 y^2.$