This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.
%I A051699 #45 Jul 02 2025 16:01:58 %S A051699 2,1,0,0,1,0,1,0,1,2,1,0,1,0,1,2,1,0,1,0,1,2,1,0,1,2,3,2,1,0,1,0,1,2, %T A051699 3,2,1,0,1,2,1,0,1,0,1,2,1,0,1,2,3,2,1,0,1,2,3,2,1,0,1,0,1,2,3,2,1,0, %U A051699 1,2,1,0,1,0,1,2,3,2,1,0,1,2,1,0,1,2,3,2,1,0,1,2,3,4,3,2,1,0,1,2,1,0,1,0,1 %N A051699 Distance from n to closest prime. %H A051699 T. D. Noe, <a href="/A051699/b051699.txt">Table of n, a(n) for n = 0..10000</a> %H A051699 Eric Weisstein's World of Mathematics, <a href="https://mathworld.wolfram.com/PrimeDistance.html">Prime Distance</a> %H A051699 <a href="/index/Di#distance_to_the_nearest">Index entries for sequences related to distance to nearest element of some set</a> %F A051699 Conjecture: S(n) = Sum_{k=1..n} a(k) is asymptotic to C*n*log(n) with C=0.29...... - _Benoit Cloitre_, Aug 11 2002 %F A051699 C = lim_{n->oo} S(n)/(n*log(n)) = 0.44 approximately. - _Ya-Ping Lu_, Apr 06 2025 %F A051699 Comment from _Giorgio Balzarotti_, Sep 18 2005: by means of the Prime Number Theorem is possible to derive the following inequality: c1*n*log(n) < S(n) < c2*n*log(n), where c1 = 1/4 and c2 = 3/8 (for n > 130). For a more accurate estimation of the values for c1 and c2, it necessary to know the number of twin primes with respect to the total number of primes. %F A051699 abs(a(n)-a(n+1)) = 1 if n != 2; a((p+q)/2 +- k) = (q-p)/2 - k, where p < q are two consecutive primes and k = 0, 1, 2, ..., (q-p)/2. - _Ya-Ping Lu_, Mar 22 2025 %e A051699 Closest primes to 0,1,2,3,4 are 2,2,2,3,3. %p A051699 A051699 := proc(n) if isprime(n) then 0; elif n<= 2 then 2-n ; else min(nextprime(n)-n, n-prevprime(n)) ; end if ; end proc; # _R. J. Mathar_, Nov 01 2009 %t A051699 FormatSequence[ Table[Min[Abs[n-If[n<2, 2, Prime[{#, #+1}&[PrimePi[n]]]]]], {n, 0, 101}], 51699, 0, Name->"Distance to closest prime." ] %t A051699 (* From version 6 on: *) a[_?PrimeQ] = 0; a[n_] := Min[NextPrime[n]-n, n-NextPrime[n, -1]]; Table[a[n], {n, 0, 104}] (* _Jean-François Alcover_, Apr 05 2012 *) %o A051699 (PARI) a(n)=if(n<1,2*(n==0),vecmin(vector(n,k,abs(n-prime(k))))) %o A051699 (PARI) a(n)=if(n<1,2*(n==0),min(nextprime(n)-n,n-precprime(n))) %o A051699 (Python) %o A051699 from sympy import prevprime, nextprime, isprime %o A051699 def A051699(n): return nextprime(n) - n if n <= 1 else 0 if isprime(n) else min(n-prevprime(n), nextprime(n)-n) # _Ya-Ping Lu_, Mar 22 2025 %Y A051699 Related sequences: A023186-A023188, A046929-A046931, A051650, A051652, A051697-A051702, A051728-A051730. %K A051699 nonn,easy,nice %O A051699 0,1 %A A051699 _N. J. A. Sloane_ %E A051699 More terms from _James Sellers_