If $2$ divides $p^2$, how does it imply $2$ divides $p$? I'm trying to understand a proof by contradiction. 
It's proving by contradiction that $\sqrt2$ isn't rational.
(It's a standard proof involving $\sqrt2=\frac{p}{q}$, where $p,q$ are already simplified integers)
There's a part of the proof that reads:
Hence $2|p^2$
But then $2|p$ because $p$ is prime.
Could someone explain how this is implied? Thanks
Edit: Would it be the same principle if it was $\sqrt5$?
 A: Simplest is to look at it like this: if you square an odd integer then you get an odd integer. Thus, if $p^2$ is even, it can only have come from squaring an even integer.
For cases like $\sqrt{5}$ you need the hint provided by Prahlad (Euclid's lemma).
Note that where you said:
"Hence $2|p^2$
But then $2|p$ because $p$ is prime."
It is actually irrelevant that $p$ be prime at that point, the important thing is that $2$ is prime.
A: To answer the more general question added in your edit: yes, one can apply a similar brute force case analysis to deduce that $\,5\mid n^2\Rightarrow 5\mid n.$ It suffices to check, mod $5,\,$ that none of $1^2,2^2,\ldots 4^2$ are $\equiv 0.\,$ Such a brute force case-analysis works for any fixed prime. But in order to prove this generally, i.e. that for all primes $\,p,\,$  it is true that $\,p\mid n^2\Rightarrow p\mid n,\,$ further properties are needed, namely the Euclidean (division) algorithm.  Iterated, it yields the Bezout Identity for the gcd, which then yields Euclid's Lemma. This implies, as a special case, the Prime Divisor Property. This then easily implies the uniqueness of prime factorizations. Below is one simple way to proceed.
Euclid's Lemma $\,\ a\mid bc \,\Rightarrow\, a\mid (a,b)c,\  $  where $\ (a,b) := \gcd(a,b)$
Proof $\ $ By Bezout, we know $ \ ja\!+\!kb\, =\, (a,b)\,$ for some integers $\,j,k,\, $ so
$\quad \color{#c00}a\mid \color{#c00}{bc} \,\Rightarrow\, \color{#c00}a\mid j\color{#c00}ac+k\color{#c00}{bc} = (ja\!+\!kb)c = (a,b)c\quad $ QED
Now, considering Euclid's Lemma in the special case  $\,a = p\,$ prime yields
$\,p\mid bc \,\Rightarrow\, p\mid \color{#0a0}{(p,b)}c,\ $ so $\ p\nmid b\,\Rightarrow\, p\mid c,\ $ by $\ \color{#0a0}{ (p,b)=1}\,$ if prime $\,p\nmid b$
Thus we have $\,\ p\mid bc \,\Rightarrow\, p\mid b\,\ \ {\rm or}\,\ \ p\mid c\ \ $  (Prime Divisor Property)
Thus we have $\,\ p\mid b^2 \,\Rightarrow\, p\mid b\ \ $ (special case $\, c = b\,$ of prior), $ $ as desired.
The Prime Divisor Property easily yields the uniqueness of prime factorizations, since it implies that we can match and cancel primes in any two factorizations. But once we have uniqueness available the irrationality of square roots of primes follows by a simple, beautiful parity argument:
$\sqrt{p} = a/b\, \Rightarrow\, p b^2\! = a^2.\, $ It follows from uniqueness that $\,pb^2$ has an odd number of factors of $\,p,\,$ but $\,a^2$ has an even number, contradicting the uniqueness of the prime factorization of $\,a^2$.
