$x^y = y^x$ for integers $x$ and $y$

This is a classic (and well known problem).

The general solution of $x^y = y^x$ is given by

$$\begin{align*}x &= (1+1/u)^u \\ y &= (1+1/u)^{u+1}\end{align*}$$

It can be shown that if $x$ and $y$ are rational, then $u$ must be an integer.

For more details, see this and this.


For every integer $n$, $x = y = n$ is a solution. So assume $x \neq y$.

Suppose $n^m = m^n$. Then $n^{1/n} = m^{1/m}$. Now the function $x \mapsto x^{1/x}$ reaches its maximum at $e$, and is otherwise monotone. Thus (assuming $n < m$) we must have $n < e$, i.e. $n = 1$ or $n = 2$.

If $n = 1$ then $n^m = 1$ and so $m = 1$, so it's a trivial solution.

If $n = 2$ then $n^m$ is a power of $2$, and so (since $m > 0$) $m$ must also be a power of $2$, say $m = 2^k$. Then $n^m = 2^{2^k}$ and $m^n = 2^{2k}$, so that $2^k = 2k$ or $2^{k-1} = k$. Now $2^{3-1} > 3$, and so an easy induction shows that $k \leq 2$. If $k = 1$ then $n = m$, and $k = 2$ corresponds to $2^4 = 4^2$.

EDIT: Up till now we considered $n,m>0$. We now go over all other cases. The solution $n = m = 0$ is trivial (whatever value we give to $0^0$).

If $n=0$ and $m \neq 0$ then $n^m = 0$ whereas $m^n = 1$, so this is not a solution.

If $n > 0$ and $m < 0$ then $0 < n^m \leq 1$ whereas $|m^n| \geq 1$. Hence necessarily $n^m = 1$ so that $n = 1$. It follows that $m^1 = 1^m = 1$. In particular, there's no solution with opposite signs.

If $n,m < 0$ then $(-1)^m (-n)^m = n^m = m^n = (-1)^n (-m)^n$, so that $n,m$ must have the same parity. Taking inverses, we get $(-n)^{-m} = (-m)^{-n}$, so that $-n,-m$ is a solution for positive integers. The only non-trivial positive solution $2,4$ yields the only non-trivial negative solution $-2,-4$.


Say $x^y = y^x$, and $x > y > 0$. Taking logs, $y \log x = x \log y$; rearranging, $(\log x)/x = (\log y)/y$. Let $f(x) = (\log x)/x$; then this is $f(x) = f(y)$.

Now, $f^\prime(x) = (1-\log x)/x^2$, so $f$ is increasing for $x<e$ and decreasing for $x>e$. So if $x^y = y^x$ has a solution, then $x > e > y$. So $y$ must be $1$ or $2$. But $y = 1$ doesn't work. $y=2$ gives $x=4$.

(I've always thought of this as the ``standard'' solution to this problem and I'm a bit surprised nobody has posted it yet.)

If $x > 0 > y$, then $0 < x^y < 1$ and $y^x$ is an integer, so there are no such solutions.

If $0 > x > y$, then $x^y = y^x$ implies $x$ and $y$ must have the same parity. Also, taking reciprocals, $x^{-y} = y^{-x}$. Then $(-x)^{-y} = (-y)^{-x}$ since $x$ and $y$ have the same parity. (The number of factors of $-1$ we introduced to each side differs by $x-y$, which is even.) So solving the problem where $x$ and $y$ are negative reduces to solving it when $x$ and $y$ are positive.