TITLE: Integers of the form $x^2-ny^2$
QUESTION [9 upvotes]: Is there any algorithm to represent the given  integers  in the form $N=x^2-ny^2$, $n>1$, say for $n=2$.
As we have for any prime, $p$,   a prime $q$ can be written as $q=x^2+py^2$,whenever $q$ is a quadratic residue of $p$ called cornacchia algorithm

REPLY [4 votes]: Example, prime $p= 159287$ 
jagy@phobeusjunior:~/old drive/home/jagy/Cplusplus$ ./indefCycle
Input three coefficients a b c for indef f(x,y)= a x^2 + b x y + c y^2 
159287  113296  20146

  0  form         159287      113296       20146  delta      2
  1  form          20146      -32712       13279  delta     -2
  2  form          13279      -20404        7838  delta     -2
  3  form           7838      -10948        3823  delta     -2
  4  form           3823       -4344        1234  delta     -2
  5  form           1234        -592          71  delta     -5
  6  form             71        -118          49  delta     -2
  7  form             49         -78          31  delta     -2
  8  form             31         -46          17  delta     -2
  9  form             17         -22           7  delta     -2
 10  form              7          -6           1  delta     -2
 11  form              1           2          -1


          85        -101
        -239         284

To Return  
         284         101
         239          85

0  form   1 2 -1   delta  -2
1  form   -1 2 1   delta  2
2  form   1 2 -1

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The discriminant is $D=8.$ A form $\langle a,b,c \rangle$ is reduced if $0 < b < \sqrt D$ AND $\sqrt D - b < 2 |a| < \sqrt D + b.$
THEOREM. The following are equivalent, when $D$ is positive and not a square, and of course $D = b^2 - 4 a c$:
(I) $0 < b < \sqrt D$ AND $\sqrt D - b < 2 |a| < \sqrt D + b.$
(II) $0 < b < \sqrt D$ AND $\sqrt D - b < 2 |c| < \sqrt D + b.$
(III) $ac < 0$ AND $b > |a+c|.$
If a form is not reduced (by any of the three tests above (I), (II), (III), we can reduce it, one step at a time, by finding the (unique) integral $\delta$ such that
$$ \sqrt D - 2 |c| < -b + 2 c \delta < \sqrt D.  $$
The next form is then $$\langle c, -b + 2 c \delta, a - b \delta + c \delta^2 \rangle$$
The change of variables matrix that accomplishes this on the level of Gram matrices is 
$$
\left(
\begin{array}{cc}
0 & -1  \\
1 & \delta 
\end{array}\right)
$$
In the example, we get from the original $\langle 159287,  113296,  20146  \rangle$ to the reduced $\langle 1,  2,  -1  \rangle$ by means of the matrix
$$
\left(
\begin{array}{cc}
85 & -101  \\
-239 & 284 
\end{array}\right)
$$
To get from $\langle 1,  2,  -1  \rangle$ to (non-reduced) $\langle 1,  0,  -2  \rangle$ we use 
$$
\left(
\begin{array}{cc}
1 & -1  \\
0 & 1 
\end{array}\right)
$$
to arrive at 
$$
\left(
\begin{array}{cc}
85 & -186  \\
-239 & 523 
\end{array}\right).
$$
Inverse 
$$
\left(
\begin{array}{cc}
523 & 186  \\
239 & 85 
\end{array}\right).
$$
So,
$$
\left(
\begin{array}{cc}
523 & 239  \\
186 & 85 
\end{array}\right)
\left(
\begin{array}{cc}
1 & 0  \\
0 & -2 
\end{array}\right)
\left(
\begin{array}{cc}
523 & 186  \\
239 & 85 
\end{array}\right) = 
\left(
\begin{array}{cc}
159287 & 56648  \\
56648 & 20146 
\end{array}\right)
$$
So $$ 523^2 - 2 \cdot 239^2 = 159287. $$
NOTE: if $n>0 $ is not a square, and $s_0 = \lfloor \sqrt n \rfloor,$ then $\langle 1, 0, -n \rangle$ is not reduced but   $\langle 1, 2 s_0,  s_0^2 - n \rangle$ is reduced. The quickest transformation  is just
$$
\left(
\begin{array}{cc}
1 & s_0  \\
0 & 1 
\end{array}\right)
$$
Three books with part or all of this are : (1929) Introduction to the Theory of Numbers, by Leonard Eugene Dickson; (1989) Binary Quadratic Forms, by Duncan A. Buell; (2007) Binary Quadratic Forms, by Johannes Buchmann and Ulrich Vollmer.