$k^2 - 5z^2 = 1 (1)$
The equation (1) is a Pell equation where $D = 5$ and the first solution $\{k, z\} =\{9, 4\} = \{p, q\} (2)$
$k_n = \dfrac{(p + q\sqrt{D})^n + (p - q\sqrt{D})^n}{2} ; z_n = \dfrac{(p + q\sqrt{D})^n - (p - q\sqrt{D})^n}{2\sqrt{D}} (3)$
Where n is index of a solution. Replace p and q in (2) with (3)
$k_n = \dfrac{(9 + 4\sqrt{5})^n + (9 - 4\sqrt{5})^n}{2} ; z_n = \dfrac{(9 + 4\sqrt{5})^n - (9 - 4\sqrt{5})^n}{2\sqrt{5}} (4)$
Equations (35,36) from Pell's equation on Wolfram
I wrote a small Python program to calculate 30 solutions. However, the first 12 solutions are correct then all others are incorrect.
import math
def pell(u,v,D,nMaxUpperBound=30):
lstResult = []
a = u + v*math.sqrt(D)
b = u - v*math.sqrt(D)
for i in range (1, nMaxUpperBound, 1) :
k = int(round((math.pow(a, i) + math.pow(b, i))/2))
z = int(round((math.pow(a, i) - math.pow(b, i))/(2 * math.sqrt(D))))
if k**2 - D*z**2 == 1:
lstResult.append((k,z))
else:
print("failed. i = ", i, "k,z => (%d,%d)" % (k,z))
return lstResult
lstResult = pell(9, 4, 5)
print(lstResult)
What should I do to improve the program or use a different approach to calculate all 30 solutions?
I also found recursive solution at Additional solutions from the fundamental solution $\displaystyle x_{k}+y_{k}{\sqrt {D}}=(x_{1}+y_{1}{\sqrt {D}})^{k}(1)$ that led them to $ \displaystyle x_{k+1}=x_{1}x_{k}+Dy_{1}y_{k} (2)$ and $\displaystyle y_{k+1}=x_{1}y_{k}+y_{1}x_{k} (3)$
First of all let's proof (1)
To do this I shall use equation from my question
$k_n = \dfrac{(p + q\sqrt{D})^n + (p - q\sqrt{D})^n}{2} ; z_n = \dfrac{(p + q\sqrt{D})^n - (p - q\sqrt{D})^n}{2\sqrt{D}} (3)$
Equations (35,36) from Pell's equation on Wolfram or see (31,32) on the same page in Wolfram.
Calculate $k_n+z_n\sqrt{D} = \dfrac{(p + q\sqrt{D})^n + (p - q\sqrt{D})^n}{2} + \dfrac{(p + q\sqrt{D})^n - (p - q\sqrt{D})^n}{2\sqrt{D}}\sqrt{D}= \dfrac{(p + q\sqrt{D})^n + (p - q\sqrt{D})^n}{2} + \dfrac{(p + q\sqrt{D})^n - (p - q\sqrt{D})^n}{2} = \dfrac{(p + q\sqrt{D})^n + (p - q\sqrt{D})^n + (p + q\sqrt{D})^n - (p - q\sqrt{D})^n}{2} = \dfrac{2(p + q\sqrt{D})^n}{2} = (p + q\sqrt{D})^n$
What was required to prove !
Here's the proof:
From (1) for $k+1$ solutions
$\displaystyle x_{k+1}+y_{k+1}{\sqrt {D}}=(x_{1}+y_{1}{\sqrt {D}})^{k+1}=(x_{1}+y_{1}{\sqrt {D}})^{k}*(x_{1}+y_{1}{\sqrt {D}}) (4)$
First parentheses in (4) represents $k$-th solution , so (4) can be rewritten as
$\displaystyle x_{k+1}+y_{k+1}{\sqrt {D}}=(x_{k}+y_{k}{\sqrt {D}})*(x_{1}+y_{1}{\sqrt {D}}) (5)$.
Then open parentheses
$x_{k+1}+y_{k+1}{\sqrt {D}} = x_kx_1+ x_ky_1\sqrt {D}+y_kx_1\sqrt {D}+y_ky_1D(6)$
Combine similar terms
$x_{k+1}+y_{k+1}{\sqrt {D}} = (x_kx_1+y_ky_1D) + \sqrt {D}(x_ky_1+y_kx_1) (7)$
Now it is easy to see that
$ \displaystyle x_{k+1}=x_{1}x_{k}+Dy_{1}y_{k} (2)$
and
$\displaystyle y_{k+1}=x_{1}y_{k}+y_{1}x_{k} (3)$
are true.
Let's find formula expressing $x_{k+2}$ by using equations (2) and (3)
from (2) $x_{k+2}=x_{1}x_{k+1}+Dy_{1}y_{k+1} (8)$
substitute $y_{k+1}$ from (3) in (8)
$x_{k+2}=x_{1}x_{k+1}+Dy_{1}(x_{1}y_{k}+y_{1}x_{k}) (9)$
simplify (9)
$x_{k+2}=x_{1}x_{k+1}+Dy_{1}x_{1}y_{k}+Dy_{1}^2x_{k} (10)$
$x_{k+2}=x_{1}(x_{k+1}+Dy_{1}y_{k})+Dy_{1}^2x_{k} (11)$
use (2) to simplify
$x_{k+2}=x_{1}(x_{k+1}+x_{k+1}-x_{1}x_{k})+Dy_{1}^2x_{k} (12)$
$x_{k+2}=x_{1}(2x_{k+1}-x_{1}x_{k})+Dy_{1}^2x_{k} (13)$
$x_{k+2}=2x_{1}x_{k+1}-x_{1}^2x_{k}+Dy_{1}^2x_{k} (14)$
$x_{k+2}=2x_{1}x_{k+1}-x_{k}(x_{1}^2-Dy_{1}^2) (15)$
You can see that in parentheses $x_{1}^2-Dy_{1}^2$ is original Pell's equation , and it equals to 1, so
$x_{k+2}=2x_{1}x_{k+1}-x_{k} (16)$
Formula (16) for $k^2-5z^2=1$ with the first solution $\{k,z\}=\{9,4\}=\{x_1,y_1\}$ gives us a recursive family of solutions $x_{k+2}=2*9*x_{k+1}-x_{k}$ or $x_{k+2}=18x_{k+1}-x_{k}$
Let's find formula expressing $y_{k+2}$ by using equations (2) and (3)
$\text{from (3) } y_{k+2}=x_{1}y_{k+1}+y_{1}x_{k+1} (17)$
substitute $x_{k+1}$ from (2)
$y_{k+2}=x_{1}y_{k+1}+y_{1}(x_{1}x_{k}+Dy_{1}y_{k}) (17)$
simplify
$y_{k+2}=x_{1}y_{k+1}+y_{1}x_{1}x_{k}+Dy_{1}^2y_{k} (18)$
get $x_k$ from (3) and apply it to (18)
$x_k=\frac{y_{k+1} - x_1y_k}{y_1} (19)$
$y_{k+2}=x_{1}y_{k+1}+y_{1}x_{1}(\frac{y_{k+1} - x_1y_k}{y_1})+Dy_{1}^2y_{k} (20)$
simplify (20)
$y_{k+2}=x_{1}y_{k+1}+x_{1}y_{k+1} - x_1^2y_k+Dy_{1}^2y_{k} (21)$
or
$y_{k+2}=2x_{1}y_{k+1} - y_k(x_1^2-Dy_{1}^2) (22)$
Again, you can see that in parentheses $x_{1}^2-Dy_{1}^2$ is original Pell's equation , and it equals to 1, so
$y_{k+2}=2x_{1}y_{k+1} - y_k (23)$
The updated program looks like this based on formulas (2) and (3)
Results:
Questions?