I want to define a $f$ function on Mathematica such as this. $f[k]$ gives the smallest $m$ holds $2k=Prime[m+1]-Prime[m]$. For example,
$$f[1]=2$$ $$f[2]=4$$ $$f[3]=9$$ $$f[4]=24$$
How can i do that?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI want to define a $f$ function on Mathematica such as this. $f[k]$ gives the smallest $m$ holds $2k=Prime[m+1]-Prime[m]$. For example,
$$f[1]=2$$ $$f[2]=4$$ $$f[3]=9$$ $$f[4]=24$$
How can i do that?
The sequence $f[k]$ you are looking for is Sloane's A038664. There is Mathematica code given there by Harvey P. Dale.
With[{d=Differences[Prime[Range[50000]]]},
Flatten[Table[Position[d, 2n, 1, 1], {n, 50}]]]
which returns {2,4,9,24,34,46,30,...}.