Q:

use contradiction to prove that the square root of $p$ is irrational

On a practice exam, our teacher provides us with this question and this answer.

Let $p$ be a prime number. Use contradiction to prove that $\sqrt{p}$ is irrational.

ANSWER: By way of contradiction, assume $\sqrt{p}$ is rational. Then there exist $a, b \in \mathbb{Z}$ with $b\neq 0$ such that $\sqrt{p} = \frac{a}{b}$. Without loss of generality, we may assume $\text{gcd}(a,b) = 1$. Then $p = \frac{a^2}{b^2}$. Thus $p | a^2$ which implies $p | a$, i.e., $\exists k \in \mathbb{Z}$ such that $p k = a$. We now have $p b^2 = (\pi k)^2 = p(p k^2)$,
so $p b^2 = a^2$.
Since $p \neq 0$, $b^2 = p k^2$, which means $p|b$. Thus $p$ is a common factor of $a$ and $b$. This is a contradiction as $a$ and $b$ are relatively prime.
My question is, how do you know to assume that the $\text{gcd} (a,b)=1$? It seems really random and I don't know why the proof jumps there. 

A:

You can make this assumption, because you lose no generality. Indeed, suppose $\text{gcd} (a,b) = d \neq 1$. Then we can write $a = d \cdot a'$ and $b = d \cdot b'$, for some relatively prime integers $a'$ and $b'$. But then
$$
\sqrt{p} = \frac{a}{b} = \frac{d a'}{d b'} = \frac{a'}{b'},
$$
so you are back in the situation that you wrote above, i.e. that $\sqrt{p}$ is a ratio of two relatively prime integers.

