%I #54 Feb 01 2024 19:06:01
%S 3,31,313,3137,31397,314159,3141601,31415899,314159257,3141592661,
%T 31415926541,314159265359,3141592653581,31415926535879,
%U 314159265358951,3141592653589771,31415926535897921,314159265358979347,3141592653589793239,31415926535897932363
%N Nearest prime to Pi*10^(n-1).
%C Lim_{n->infinity} a(n)/10^(n-1) = Pi.
%C Demonstration: If gap_a(p) (resp. gap_b(p)) denotes the gap between a prime p and the next (resp. preceding) prime, we have by definition |Pi*10^(n-1)-a(n)| < max(gap_a(a(n)),gap_b(a(n)))/2. Now, it is known from results on prime gaps (e.g., Ingham, 1937) that gap_a(p) and gap_b(p) are O(p^theta) for some theta < 1; thus |Pi*10^(n-1)-a(n)| = O(a(n)^theta) and the result.
%H Alois P. Heinz, <a href="/A247351/b247351.txt">Table of n, a(n) for n = 1..300</a>
%p a:= proc(n) local f, h, p, q; Digits:= 20+n;
%p f:= evalf(Pi*10^(n-1)); h:= round(f);
%p if isprime(h) then return h fi;
%p p:= prevprime(h); q:= nextprime(h);
%p `if`(f-p < q-f, p, q)
%p end:
%p seq(a(n), n=1..30); # _Alois P. Heinz_, Sep 24 2014
%t a[n_]:=If[10^(n-1)*Pi<1/2(NextPrime[10^(n-1)*Pi,-1]+NextPrime[10^(n-1)*Pi]),NextPrime[10^(n-1)*Pi,-1],NextPrime[10^(n-1)*Pi]];Table[a[n],{n,20}] (* _Farideh Firoozbakht_, Sep 18 2014, Sep 24 2014 *)
%t np[n_]:=With[{c=Pi 10^n},Nearest[{NextPrime[c,-1],NextPrime[c]},c]]; Array[np,20,0]//Flatten (* _Harvey P. Dale_, Feb 01 2024 *)
%o (Python)
%o from sympy import isprime, nextprime, prevprime, S
%o def a(n):
%o target = int(S.Pi*10**(n-1))
%o if isprime(target): return target
%o before, after = prevprime(target), nextprime(target)
%o return before if target-before <= after-target else after
%o print([a(n) for n in range(1, 21)]) # _Michael S. Branicky_, Mar 31 2021
%Y Cf. A000796 (Pi).
%K nonn
%O 1,1
%A _Jean-Christophe Hervé_, Sep 18 2014
%E a(7)-a(14) from _Farideh Firoozbakht_, Sep 18 2014
%E a(15)-a(20) from _Alois P. Heinz_, Sep 24 2014