Proving the real numbers are complete In Rudin's book, the following proof is published:

Let $A$ be the set of all positive rationals $p : p^2 < 2$. Let $B$ be the set of all positive rationals $p : p^2 > 2$. $A$ contains no largest number and $B$ contains no smallest.
Let q be a rational. More explicitly, $$\forall p \in A, \exists q \in A : p < q$$
and $$\forall p \in B, \exists q \in B : q < p$$
This needs proof.

Now he introduces a couple of equations which are not clear where they are derived from. As follows,

To do this, we associate with each rational $p > 0$ the number $$q = p - {\frac{p^2-2}{p+2}} = {\frac{2p+2}{p+2}}$$
then $$q^2 - 2 = {\frac {2(p^2-2)}{(p+2)^2}}$$

He then goes on to prove how $p \in A \rightarrow q \in A$; similarly for the set B.
My question is: How did he arrive at those equations - particularly, the first one (since the second is clear)?

if $p^2<2$
let's add 2p to both sides of the inequality
then $p^2+2p<2+2p$ then $p(p+2)<2(p+1)$ then $p<\frac{2(p+1)}{p+2}$ so let's set $q=\frac{2(p+1)}{p+2}$ then we have if $p^2<2$ then $p<q$. what is left to show is that $q^2<2$
and that would show that $0<p<q$ and $q \in A$
