Solution (source code)

= Solution

For the <relativistic head-on elastic collision with a stationary target>, use $c=1$ as stipulated, so <masses> are measured in <energy> units. The incoming <momentum> is $p=\sqrt{E^2-M^2}$ and the invariant squared total <four-momentum> is
$$
s=(E+m)^2-p^2=M^2+m^2+2mE.
$$
The <centre-of-mass frame> moves at $\beta_*=p/(E+m)$, with $\gamma_*=(E+m)/\sqrt s$. In this frame the incident particle has
$$
E_M^*=\frac{M^2+mE}{\sqrt s},\qquad p_M^*=\gamma_*(p-\beta_*E)=\frac{mp}{\sqrt s}.
$$
A nontrivial head-on <elastic collision> reverses $p_M^*$ and preserves $E_M^*$. Boosting back to the <laboratory frame> gives
$$
E'=\gamma_*(E_M^*-\beta_*p_M^*)=\frac{(E+m)(M^2+mE)-m(E^2-M^2)}s,
$$
$$
\boxed{E'=\frac{2mM^2+E(m^2+M^2)}{2Em+m^2+M^2}.}
$$
The no-interaction forward branch instead leaves $E'=E$; the requested formula describes the actual one-dimensional encounter and recoil.