I am trying to understand the proof for this.
As I understand you take $a=[a_0,a_1....]$ and $r_n=[a_n,a_{n+1}...]$ thus $a=[a_0,a_1,a_2,....r_n] = \frac{r_np_{n-1}+p_{n-2}}{r_nq_{n-1}+q_{n-2}}$
we then substitute this into the quadratic equation $f(x) = x^2-K$ and get that the discriminant equals 4K. However after that I'm a bit puzzled.
sources: http://modular.math.washington.edu/edu/124/lectures/lecture19/lecture19/node2.html http://math.ucsb.edu/~jcs/PeriodicContinuedFractions.pdf