TITLE: General solution of Pell's equation
QUESTION [7 upvotes]: If we know the minimal solution or any specific solution  of Pell's equation $x^2-ny^2=1$ , is there is any general formula to write all solution of Pell's equation?

REPLY [2 votes]: Ummm; if you have $u,v$ the smallest positive (nonzero) solution to $u^2 - n v^2 = 1,$ then as in the answer by Stefan4024, all solutions are obtained from the $(1,0)$ solution by the mapping
$$ (x,y) \mapsto (ux + nvy, \; v x + u y).   $$
The Cayley-Hamiltion Theorem says that we can write separate linear recurrences for $x,y.$ That is
$$  x_0 = 1, x_1 = u, x_2 = 2 u^2 - 1,$$
and
$$ x_{n+2} = 2 u x_{n+1} - x_n. $$
$$  y_0 = 0, y_1 = v, y_2 = 2 u v,$$
and
$$ y_{n+2} = 2 u y_{n+1} - y_n. $$
The full proof that this gives all solutions is rather long. Maybe I should just draw attention to alternatives. All solutions to $x^2 - 5 y^2 = 6061$ make up a rather more complicated set; in the sense discussed above, there are eight orbits, not just one.
jagy@phobeusjunior:~$ ./Pell_Target_Fundamental
  Automorphism matrix:  
    9   20
    4   9
  Automorphism backwards:  
    9   -20
    -4   9

  9^2 - 5 4^2 = 1

 x^2 - 5 y^2 = 6061

Sun Jul  3 14:58:33 PDT 2016

x:  79  y:  6 ratio: 13.1667  SEED   KEEP +- 
x:  81  y:  10 ratio: 8.1  SEED   KEEP +- 
x:  129  y:  46 ratio: 2.80435  SEED   KEEP +- 
x:  159  y:  62 ratio: 2.56452  SEED   KEEP +- 
x:  191  y:  78 ratio: 2.44872  SEED   BACK ONE STEP  159 ,  -62
x:  241  y:  102 ratio: 2.36275  SEED   BACK ONE STEP  129 ,  -46
x:  529  y:  234 ratio: 2.26068  SEED   BACK ONE STEP  81 ,  -10
x:  591  y:  262 ratio: 2.25573  SEED   BACK ONE STEP  79 ,  -6
x:  831  y:  370 ratio: 2.24595
x:  929  y:  414 ratio: 2.24396
x:  2081  y:  930 ratio: 2.23763
x:  2671  y:  1194 ratio: 2.23702
x:  3279  y:  1466 ratio: 2.2367
x:  4209  y:  1882 ratio: 2.23645
x:  9441  y:  4222 ratio: 2.23614
x:  10559  y:  4722 ratio: 2.23613
x:  14879  y:  6654 ratio: 2.2361
x:  16641  y:  7442 ratio: 2.23609
x:  37329  y:  16694 ratio: 2.23607
x:  47919  y:  21430 ratio: 2.23607
x:  58831  y:  26310 ratio: 2.23607
x:  75521  y:  33774 ratio: 2.23607
x:  169409  y:  75762 ratio: 2.23607
x:  189471  y:  84734 ratio: 2.23607
x:  266991  y:  119402 ratio: 2.23607
x:  298609  y:  133542 ratio: 2.23607
x:  669841  y:  299562 ratio: 2.23607
x:  859871  y:  384546 ratio: 2.23607
x:  1055679  y:  472114 ratio: 2.23607
x:  1355169  y:  606050 ratio: 2.23607
x:  3039921  y:  1359494 ratio: 2.23607
x:  3399919  y:  1520490 ratio: 2.23607
x:  4790959  y:  2142582 ratio: 2.23607
x:  5358321  y:  2396314 ratio: 2.23607
x:  12019809  y:  5375422 ratio: 2.23607
x:  15429759  y:  6900398 ratio: 2.23607
x:  18943391  y:  8471742 ratio: 2.23607
x:  24317521  y:  10875126 ratio: 2.23607

Sun Jul  3 14:59:13 PDT 2016

 x^2 - 5 y^2 = 6061

jagy@phobeusjunior:~$ 
jagy@phobeusjunior:~$