Find all functions $f:\mathbb{P}\rightarrow\mathbb{P}$ such that: $f(p)^{f(q)}+q^p=f(q)^{f(p)}+p^q$ holds for all $p,q\in\mathbb{P}$. $\blacksquare$ Problem: Let $\mathbb{P}$ be the set of all prime numbers. Find all functions $f:\mathbb{P}\rightarrow\mathbb{P}$ such that:
$$f(p)^{f(q)}+q^p=f(q)^{f(p)}+p^q$$holds for all $p,q\in\mathbb{P}$.

Note:
The immediate solution that comes to mind is $f(p) = p~\forall~p \in \mathbb{P}$. But we need to show that it's the only solution.

But I'm unable to find any elegant ways to find the solution. Any sort of help will be appreciated.
 A: I thought it would be helpful to readers to have a condensed solution whose structure is easy to see in one go. But everything in this answer is part of Ralph Clausen's solution.
First, we show that $f$ is injective. If $f(p)=f(q)$, then the functional equation implies
$$
q^p = \big( f(q)^{f(p)} + p^q \big) - f(p)^{f(q)} = p^q,
$$
which implies $p=q$ by unique factorization.
Next, we show that if $p$ is an odd prime then $f(p)$ is also odd. If $f(p)=2$, then $f(q)\ne2$ for any other odd prime $q$ by injectivity; but then looking at the functional equation modulo $2$ gives the contradiction
$$
f(q)^{f(p)} + p^q \equiv 0 \not\equiv f(p)^{f(q)} + q^p \pmod 2.
$$
A similar argument shows that f(2)=2: if not, then $f(2)$ is odd, but then for any odd prime $p$, we get the contradiction
$$
f(2)^{f(p)} + p^2 \equiv 0 \not\equiv f(p)^{f(2)} + 2^p \pmod 2.
$$
Finally, the functional equation with $2$ and any odd prime $q$ gives
$2^{f(q)} + q^2 = f(q)^2 + 2^q$,
or equivalently
$$
2^{f(q)} - f(q)^2 = 2^q - q^2.
$$
But $\Phi(n) = 2^n - n^2$ is a strictly increasing function on integers $n\ge3$ (as can be seen by examining $\Phi(n+1)-\Phi(n)$), and therefore this last equation implies that $f(q)=q$ for any odd prime $q$.
A: $\DeclareMathOperator{\cA}{\mathcal{A}} \DeclareMathOperator{\cB}{\mathcal{B}} \DeclareMathOperator{\cC}{\mathcal{C}} \DeclareMathOperator{\cD}{\mathcal{D}} \DeclareMathOperator{\cE}{\mathcal{E}} \DeclareMathOperator{\cF}{\mathcal{F}} \DeclareMathOperator{\cG}{\mathcal{G}} \DeclareMathOperator{\cH}{\mathcal{H}} \DeclareMathOperator{\cI}{\mathcal{I}} \DeclareMathOperator{\cJ}{\mathcal{J}}\DeclareMathOperator{\cK}{\mathcal{K}}\DeclareMathOperator{\cL}{\mathcal{L}}\DeclareMathOperator{\cM}{\mathcal{N}}\DeclareMathOperator{\cO}{\mathcal{O}}\DeclareMathOperator{\cP}{\mathcal{P}}\DeclareMathOperator{\cQ}{\mathcal{Q}}\DeclareMathOperator{\cR}{\mathcal{S}}\DeclareMathOperator{\cT}{\mathcal{T}} \DeclareMathOperator{\cU}{\mathcal{U}} \DeclareMathOperator{\cV}{\mathcal{}} \DeclareMathOperator{\cA}{\mathcal{A}} \DeclareMathOperator{\cA}{\mathcal{A}} \DeclareMathOperator{\bbZ}{\mathbb{Z}} \DeclareMathOperator{\bbP}{\mathbb{P}} \DeclareMathOperator{\bbN}{\mathbb{N}} \DeclareMathOperator{\bbO}{\mathbb{O}} \DeclareMathOperator{\bbE}{\mathbb{E}} \DeclareMathOperator{\bbR}{\mathbb{R}} $
$\color{Purple}{\textbf{Notation:}}$ the notations are:

*

*$\bbZ_{k} :=$ The set of naturals $\geqslant k$.


*$\bbE :=$ The set of all even numbers.


*$\bbO :=$ the set of all odd numbers.


*$\bbP :=$ the set of all prime numbers.


*$\bbP_{\bbO} := $ the set of all odd primes.


*$\cD(g) := $ the domain of the map $g$.

In order to solve the problem, we will use and prove some Lemmas.
$\bullet~$Lemma(1): The map $f$ is not constant map.
$\bullet~$Proof. Let's assume on the contrary we have that $f \equiv p' \in \bbP$. Thus, we have that for any $p\neq q \in \bbP$:
$$ p'^{p'} + q^p = p'^{p'} + p^q \implies q^p = p^q $$
Clearly a contradiction as $p \neq q \in \bbP$. Thus, we have proved our Lemma. $\qquad \qquad \blacksquare$
Thus, we have $f$ is a non-constant map.
Now, let's recall an interesting fact about $\bbP$. It can be partitioned into the sets $\{2 \}$ and $\bbP_{\bbO}$. Now, we have this partition cause maybe it'll be helpful by using the even-odd argument.
$\bullet~$Lemma(2): There are no two $~p_0, q_0 \in \bbP_{\bbO}$ such that $p_0 \neq q_0$ and $f(p_0) = f(q_0) = 2$.
$\bullet~$Proof. On the contrary let's assume that there exists such a tuple $(p_0, q_0) \in \bbP_{\bbO}^2 $. Thus we have that
$$ f(p_0)^{f(q_0)} + q_0^{p_0} = f(q_0)^{p_0} + p_0^{q_0}  $$
$$ \implies 2^2 + q_0^{p_0} = 2^2 + p_0^{q_0} \implies q_0^{p_0} = p_0^{q_0} \quad [\Rightarrow \Leftarrow] $$
Thus, no two $p_0, q_0 \in \bbP_{\bbO}$ exists such that $f(p_0) = f(q_0) =2$. this completes the proof. $\qquad \blacksquare$
Thus, from Lemma(2) we have that there may exist a $p_0 \in \bbP_{\bbO}$ such that $f(p_0) =2$. The next lemma will take care of that case.
$\bullet~$Lemma(3): There is no such $p_0 \in \bbP_{\bbO}$ such that $f(p_0) = 2$.
$\bullet~$Proof. Assume such a $p_0 \in \bbP_{\bbO}$ exists. Consider the tuple $(p_0, q)$ for any $q \neq p_0 \in \bbP_{\bbO}$. Then we have:
$$ f(p_0)^{f(q)} + q^{p_0} = f(q)^{f(p_0)} + p_0^q $$
$$ \implies  2^{f(q)} + q^{p_0} = f(q)^2 + p_0^q $$
Let's note that, the R.H.S $\in \bbE$ and the L.H.S $\in \bbO$. A contradiction. Thus proved! $ \quad \blacksquare$
Now, Lemma(3) implies that $p \overset{f}{\not\mapsto} 2~$ for any $p \in \bbP_{\bbO}$. Thus the only chance left is $\bbP \ni 2 \overset{f}{\mapsto} 2 \in \bbP$. We'll show that it holds. But at first let's observe that there can be an extension of Lemma(1). We'll discuss it in our next lemma.
$\bullet~$Lemma(4): There exists no $f~$ for $p' \in \bbP$ satisfying:
$$ f(p') = \begin{cases} q' & \text{for } p' = 2 \\ q_0 & \text{for all } p' \in \bbP_{\bbO}
\end{cases}$$
Where $q_0 \in \bbP_{\bbO}$ by Lemma(3).
$\bullet~$Proof. On the contrary let's assume such a map $f \in \text{End}(\bbP, \bbP)$ such that the conditions in the Lemma are satisfied. Let's take the tuple $(p_0, p_1) \in \bbP_{\bbO}^2$ such that $p_0 \neq p_1$. Thus we have that:
$$ f(p_0)^{f(p_1)} + p_1^{p_0} = f(p_1)^{f(p_0)} + p_0^{p_1} $$ $$ \implies q_0^{q_0} + p_1^{p_0} = q_0^{q_0} + p_0^{p_1} \implies p_1^{p_0} = p_0^{p_1} \quad [\Rightarrow \Leftarrow] $$
Thus, we have got a contradiction. Hence our lemma is proved. $\qquad \qquad \blacksquare$
Now, we have another small lemma, which will basically help us see the solution.
$\bullet~$Lemma(5): For a map $f$ satisfying the functional equation, $f(2) = 2$.
$\bullet~$Proof. From Lemma(3) we have that no $~\bbP_{\bbO} \ni p_0 \overset{f}{\mapsto} 2$. Then let's consider the tuple $(p_0, 2)$. Thus, we have that:
$$ f(p_0)^{f(2)} + 2^{p_0} = f(2)^{f(p_0)} + p_0^2 $$
Now, note that, if $f(2) \in \bbO$, then R.H.S $\in \bbE$ and L.H.S $\in \bbO$. Thus, $f(2) \in \bbE$. And as we know that only $\bbP\ni 2\in \bbE$. Thus, $f(2) = 2$. This completes the proof. $\qquad \qquad \blacksquare$
Now, as we have $f(2) = 2$, let's consider some $p \in \bbP_{\bbO}$ and consider the tuple $(p, 2)$. Then
by Lemma(5) we have that:
$$ f(p)^2 + 2^p = 2^{f(p)} + p^2 \implies q^2 + 2^p = 2^q + p^2 \quad [\text{Let } f(p)= q \in \bbP_{\bbO}]   $$
Let's consider the map $\Phi(x) = 2^x - x^2 ~\forall~x \in \bbR$. then we have $\Phi|_{\bbN_{> 2}}(n) = 2^n - n^2$ for $n \in \bbN_{> 2}$. Now we have the last and the conclusive lemma to solve the problem.
$\bullet~$Lemma(6): The map $\Phi|_{\bbN_{>2}}$ is a strictly increasing function.
$\bullet~$Proof. So, essentially we just need to show that $\Phi|_{\bbN_{> 2}}(n + 1) > \Phi|_{\bbN_{ > 2}}(n) ~\forall~n \in \bbN_{> 2}$. Thus, we have that:
$$ \Phi|_{\bbN_{ > 2}}(n + 1) - \Phi|_{\bbN_{>2}}(n) = (2^{n + 1} - 2^{n} ) - \left( (n +1)^2 - n^2 \right)= 2^n -(2n +1)  $$
Now, $\Phi|_{\bbN_{>2}}(n +1) >\Phi|_{\bbN_{>2}}(n) \iff 2^n > 2n + 1 ~\forall~n \in \bbN_{>2}$, and to prove $2^n > 2n +1~$ is immidiate by induction, hence the proof is ommitted. Thus we have that $\Phi|_{\bbN_{> 2}}(n + 1) > \Phi|_{\bbN_{>2}}(n) \implies \Phi|_{\bbN_{>2}} \nearrow $. This completes the proof. $ \qquad \qquad \blacksquare$
Now, as we have $\cD\left(\Phi|_{\bbP_{>2}}\right) = \bbP_{\bbO} \subset \bbN_{> 2} = \cD\left( \Phi|_{\bbN_{>2}}\right)$ and $\Phi|_{\bbN_{>2}}$ is increasing, thus, $\Phi|_{\bbP_{>2}} \nearrow $. Thus, by Lemma(6), if $\bbP_{\bbO} \ni p \neq q \in \bbP_{\bbO}$ then  $\Phi|_{\bbP_{>2}}(p) \neq  \Phi|_{\bbP_{>2}}(q) $.
But for $p \neq q$ we have $$ 2^p + q^2 = 2^q + p^2 \implies \Phi|_{\bbP_{>2}}(p) = \Phi|_{\bbP_{> 2}}(q)$$
Hence, we have that $$ p = q \implies f(p) = p \quad [\text{as } f(p) = q] $$
As $p \in \bbP_{\bbO}$ was arbitrary, we have that $f(p)=p~\forall~p\in \bbP_{\bbO}$. Thus, we finally have that:
$$f(p)=p~\forall~p\in \bbP$$
