Q:

If $k, x, y \in \mathbb{Z}$ and $k = \frac{x}{y} + \frac{y}{x}$, why is $k=2$?

I was presented with this problem:
$$x,y \in \mathbb{Z}$$
$$k = \frac{x}{y} + \frac{y}{x}$$
$$\text{Prove that if } k \in \mathbb{Z}\text{, then } k = 2.$$
I began by rewriting the equation.
$$k = \frac{x^2 + y^2}{xy}$$
I then reasoned that if k is an integer, then
$$x^2 + y^2 = nxy$$
where $n \in \mathbb{Z}$. I continued to rewrite this to find some relation.
$$x^2 - nxy + y^2 = 0$$
$$x^2 - 2xy + y^2 = (n-2)xy$$
$$(x-y)^2 = (n-2)xy$$
I figured this proved that $(n-2)xy \in \mathbb{Z}^2$, but I'm not sure how to use this fact to help me.

A:

Suppose that $\gcd(x,y)=d$ and $x=dx_0$, $y=dy_0$. Then $\gcd(x_0,y_0)=1$ and 
$$k=\frac{x_0}{y_o}+\frac{y_0}{x_0}$$
\begin{align*}
(x_0-y_0)^2=(k-2)x_0y_0
\end{align*}
Suppose that $x_0>1$. Let $p$ be a prime factor of $x_0$, then $x_0$ is a prime factor of $(x_0-y_0)^2$ and hence is a prime factor of $x_0-y_0$. Therefore, $p$ is a prime factor of $y_0$. This leads to a contradiction.
Therefore, $x_0=1$. Skimilarly, $y_0=1$.
$k=2$.

