Nearest Prime

Author

Eric W. Weisstein
October 17, 2002

This notebook downloaded from http://mathworld.wolfram.com/notebooks/PrimeNumbers/NearestPrime.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/NearestPrime.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Code

rounddown[l_List]:=Position[Abs[l],Min[Abs[l]]][[1,1]]
roundup[l_List]:=Position[Abs[l],Min[Abs[l]]][[-1,1]]

Ties to Smaller

A051697

n1=Table[
Function[n,p=If[n<2,{2},Prime[{#,#+1}&[PrimePi[n]]]];p[[rounddown[n-p]]]][n],{n,0,1001}];

Take[n1,10]

Ties to Larger

n2=Table[
Function[n,p=If[n<2,{2},Prime[{#,#+1}&[PrimePi[n]]]];p[[roundup[n-p]]]][n],{n,0,1000}];

Take[n2,10]

Union[Drop[n1,4]-Drop[n2,3]]