# In the following proof that square root of 2 is non-terminating, where does the expression for q come from? [duplicate]

I understand the following proof but how did the author come up with the following expression? $$q = p - \frac{p^2 -2}{p + 2}$$ $q$ being defined that way is crucial for the proof but what's so special about that choice of $q%$? Why choose that?
The expression for $q^2 - 2$ follows from the first part but I don't understand how to get $q$ itself. The best I could do was realize that, for this particular case, $q = \sqrt{2} + 1$ but I can't see how that's significant and ow that could have led to the expression I'm curious about.
Also what if I need to do the same thing for an arbitrary root $\sqrt{n}$. How do I come up with such a $q$?
P.S.: I'm talking about the second part of the proof- not where he proves that $\sqrt{2}$ is irrational but the part "... A contains no largest number and ...".
This question was merged with No maximum(minimum) of rationals whose square is lesser(greater) than $2$. because it is an exact duplicate of that question.