Let d = 7. √7 has a periodic continued fraction of the form: [2, (1, 1, 1, 4)]. So r= 4 (r is the period). Notice that r is even.
After a lot of research I found out that:
If period is even, then x1 = hnr-1 and y1 = knr-1 , n = 1,3,5,7 ... where hn/kn are the convergents of the expansion of √7 and x1, y1 the minimal solution of Pell's equation, x2 - 7y2 = 1.
If period is odd, then x1 = hnr-1 and y1 = knr-1 , n = 2,4,6,8 ...
Eg. For d = 7, period is even (=4) so x1 = h1*4-1 = h3 and y1 = k1*4-1 = k3. That means the solution (x1, y1) can be obtained by the 3rd convergent of √7.
x1 can be found very easily from a mathematical aspect.
1st convergent: 2 + 1 / 1 = (2 * 1 + 1) / 1 = 3/1. So h1 = 3 and k1 = 1.
2nd convergent: 2 + 1 / (1 + 1 / 1) = 2 + 1 / (1 + 1) = 2 + 1 / 2 = (2 * 2 + 1) / 2 = 5/2 . So h2 = 5 and k2 = 2.
3rd convergent: With the same way we prove that h3 = 8 and k3 = 3.
So x1 = h3 = 8 and y1 = k3 = 3.
The mathematical way is pretty straight-forward.
I am really stuck trying to implement an algorithm which calculates hn and kn.
- How can I implement the calculation of the n-th convergent when the sequence [2, (1, 1, 1, 4)] is known ?
I found solution to my problem from this paper: https://www.math.ru.nl/~bosma/Students/CF.pdf
You can calculate any convergent from this formula: pn/qn = (an * pn-1 + pn-2) / (an * qn-1 + qn-2)