For every prime number $p$, the function ${\rm ms_2}(p)$ gives the smallest prime number that results in a power of 2 when added to $p$.
For example: ${\rm ms_2}(857) = 167$, since $857+167 = 1024 = 2^{10}$.
What is wrong with this Mathematica code?
ms2[n_] := Module[{p}, Do[If[PrimeQ[p = NextPrime[n]] && 2^n - p == 1, Return[p]], {p, n + 1}]] ## 
pranging from1ton+1from theDo, but are also trying to assignpto beNextPrime[n](which will never change). You probably don't want both of those. $\endgroup$