OFFSET

0,1

LINKS

Eric Weisstein's World of Mathematics, Nearest Prime

EXAMPLE

Closest primes to 0,1,2,3,4 are 2,2,2,3,3.

MATHEMATICA

a[n_] := (np = NextPrime[n]; pp = Prime[PrimePi[np] - 1]; Which[np > 2n-pp, pp, np < 2n-pp, np, True, pp]); a[0] = a[1] = 2; Table[a[n], {n, 0, 71}] (* Jean-François Alcover, Jul 28 2011 *)

PROG

(PARI) a(n)=if(n<3, return(2)); my(p=precprime(n), q=nextprime(n)); if(q-n<n-p, q, p) \\ Charles R Greathouse IV, Apr 28 2015

CROSSREFS

KEYWORD

nonn,easy

AUTHOR

EXTENSIONS

More terms from James A. Sellers

STATUS

approved