%I #18 Dec 13 2022 07:55:30
%S 2,2,3,7,17,31,61,127,257,509,1021,2053,4093,8191,16381,32771,65537,
%T 131071,262147,524287,1048573,2097143,4194301,8388617,16777213,
%U 33554467,67108859,134217757,268435459,536870909,1073741827,2147483647,4294967291,8589934583
%N Prime nearest to 2^n. In case of a tie, choose the smaller.
%H Harvey P. Dale, <a href="/A117387/b117387.txt">Table of n, a(n) for n = 0..1000</a>
%F a(n) = A000079(n) - A059959(n). [Corrected by _Georg Fischer_, Dec 13 2022]
%t f[n_] := Block[{k = 0}, While[ !PrimeQ[2^n - k] && !PrimeQ[2^n + k], k++ ]; Min@Select[{2^n - k, 2^n + k}, PrimeQ@# &]]
%t pn2n[n_]:=Module[{c=2^n,a,b},a=NextPrime[c,-1];b=NextPrime[c];If[b-c < c-a,b,a]]; Join[{2,2},Table[pn2n[n],{n,2,40}]] (* _Harvey P. Dale_, Jul 24 2019 *)
%o (Python)
%o from sympy import prevprime, nextprime
%o def A117387(n): return (m if (m:=nextprime(k:=1<<n)) < (k<<1)-(r:=prevprime(k)) else r) if n>1 else 2 # _Chai Wah Wu_, Aug 08 2022
%K nonn
%O 0,1
%A _Lekraj Beedassy_, Mar 11 2006
%E Edited, corrected and extended by _Robert G. Wilson v_, Mar 14 2006