login
Closest prime to n (break ties by taking the smaller prime).
24

%I #17 Apr 28 2015 17:23:52

%S 2,2,2,3,3,5,5,7,7,7,11,11,11,13,13,13,17,17,17,19,19,19,23,23,23,23,

%T 23,29,29,29,29,31,31,31,31,37,37,37,37,37,41,41,41,43,43,43,47,47,47,

%U 47,47,53,53,53,53,53,53,59,59,59,59,61,61,61,61,67,67,67,67,67,71,71

%N Closest prime to n (break ties by taking the smaller prime).

%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/NearestPrime.html">Nearest Prime</a>

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

%t 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 *)

%o (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

%Y Related sequences: A023186-A023188, A046929-A046931, A051650, A051652, A051697-A051702, A051728-A051730.

%K nonn,easy

%O 0,1

%A _N. J. A. Sloane_

%E More terms from _James A. Sellers_