Let $n$ be an integer. If $n^2$ is divisible by $3$, then $n$ is divisible by $3$. I am trying to prove the statement "Let $n$ be an integer. If $n^2$ is divisible by $3$, then $n$ is divisible by $3$." by using the direct proof technique.
The following is my proof.

Consider the following definition.
Definition 5.1: A nonzero integer $a$ divides an integer $b$ if there is an integer $j$ such that $b = aj.$
Because $3$ divides $n^2$, by Definition 5.1 it follows that
\begin{align}
n^2 = 3j \text{ for some $j \in \mathbb{Z}$}
\end{align}
By taking the square root on both sides of the above equation, one obtains
\begin{align}
n = \pm \sqrt{3j} \text{ for some $j \in \mathbb{Z}$}
\end{align}
Since $n$ is an integer, for the $RHS$ of the above equation to be an integer, $j = 3k^2$ for each $k \in \mathbb{Z^*}$.
Hence,
\begin{align}
n = \pm \sqrt{9k^2} = \pm 3k \text{ for each $k \in \mathbb{Z^*}$}
\end{align}
By Definition 5.1, one can conclude that $3$ divides $n$.

Is the proof correct?

Reference:
Reading, Writing, and Proving: a Closer Look at Mathematics, by Ulrich Daepp and Pamela Gorkin, 2nd ed., Springer, 2011, pp. 52,55.
 A: Your proof has a problem here

Since $n$ is an integer, for the $RHS$ of the above equation to be an integer, $j = 3k^2$ for each $k \in \mathbb{Z^*}$.

How do you know this?
A: Your error was noted in the other answer.

You can apply Proof by Contradiction:

*

*If $n=3k-1$, then $(3k-1)^2=9k^2-6k+1.$
This implies,
$$(3k-1)^2 \equiv 1 ~ \text{mod}~3$$

*

*If $n=3k-2$, then $(3k-2)^2=9k^2-12k+4.$
This implies,
$$(3k-2)^2 \equiv 1 ~ \text{mod}~3$$
Thus, it must be if and only if $n=3k$.
$$9k^2 \equiv 0~ \text{mod}~3$$

Generalization:
Let, $m$ be a prime number.
If, $n^2 \equiv 0~ \text{mod}~m$ , then if and only if $n=mk, k\in\mathbb Z.$
A: Welcome to MSE! Here's an alternate elementary proof
$$n^2 \equiv 0 \pmod{3} \implies 3 \mid n^2 $$
Thus  there's a factor of $3$ in $n^2$. Now it's very trivial that power of any prime in a perfect square is even, and hence power of $3$ in $n^2$ is even which implies that $3 \mid n$ and this concludes the proof. You can even generalize this for any number. Hope this helps.
