1 | initial version |
Via the reference within this page: http://www.numbertheory.org/php/surd.html
I found a more elegant algorithm than the one sketched above:
Algorithm for finding the period of a quadratic irrational
Input: $(a,b,c)\in Z \times N\times Z$ and $b$ not a perfect square. This triple represents $\frac{a+\sqrt{b}}{c}$.
If $c$ does not divide $b-a^{2}$ then multiply everything by $\frac{|c|}{|c|}$ , and get new triple that we call $(a',b',c')$
Set $P_{0}=a'$ , $Q_{0}= c'$, $d=c'$.
For $k\geq0$ set
(a) $\alpha_{k}=\frac{P_{k}+\sqrt{d}}{Q_{k}}$
(b) $a_{k}=\lfloor\alpha_{k}\rfloor$
(c) $P_{k+1}=a_{k}Q_{k}-P_{k}$
(d) $Q_{k+1}=\frac{d-P_{k+1}^{2}}{Q_{k}}$
Then $\alpha_{0}=\frac{a+\sqrt{b}}{c}=[a_{0},a_{1},a_{2},...]$
Let $k< l$ be the first pair of integers which satisfy that $$(P_{k},Q_{k})=(P_{l},Q_{l})$$ then the period of $\alpha_{0}$ is $(a_{k},\dots,a_{l-1})$ .
Hopefully this will help implementing it...