<s>[INST] How to solve pell type equation Example $x^2-7y^2=2$
Find $x,y$ in general when $x,y$ are integer
I don't know how to solve it , I need the method to help to solve it
 [/INST] Apply the PQa algorithm for D=7, determining that $\sqrt{7}=[2,\overline{1,1,1,4}]$
$
\begin{array}{c|c|c|c|c|c}
  j & P_j & Q_j & a_j & A_j & B_j \\
  \hline
  0 & 0 & 1 & 2 & 2 & 1\\
  1 & 2 & 3 & 1 & \textbf{3} & \textbf{1}\\
  2 & 1 & \textbf{2} & 1 & 5 & 2\\
  3 & 1 & 3 & 1 & \textbf{8} & \textbf{3}\\
  4 & 2 & \textbf{1} & 4 & 37 & 14\\
  \end{array}
$
From this (bold highlighted cells), we see that $3^2-7 \cdot 1^2 = 2$ and $8^2-7 \cdot 3^2=1$.
All solutions $(x_n,y_n)$ can then be found via
$$x_n+y_n\sqrt{7} = (3+\sqrt{7}) \cdot (8+3\sqrt{7})^n$$
or via
$$\begin{pmatrix}x_n&y_n\\7y_n&x_n\end{pmatrix} = \begin{pmatrix}3&1\\ 7&3\end{pmatrix} \begin{pmatrix}8&3\\ 21&8\end{pmatrix}^n$$
Or, equivalently (taking products of this type of matrices is commutative, and removing the redundancy)
$$\begin{pmatrix}y_n\\x_n\end{pmatrix} = \begin{pmatrix}8&3\\ 21&8\end{pmatrix}^n\begin{pmatrix}1\\ 3\end{pmatrix} $$
Which is the same (but upside down) what Jagy mentions.
Of course, this only works for $x^2-Dy^2 = k$ when $k < 2\sqrt{D}$. For general $k$, other methods are required (see Jagy's answer).
</s>