Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Compute Ramanujan primes
| ResourceFunction["RamanujanPrimes"][n] returns the first n Ramanujan primes. | 
Compute the first twenty Ramanujan primes:
| In[1]:= | ![ResourceFunction["RamanujanPrimes"][20]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/1522cbc6a5a9c12b.png) | 
| Out[1]= |  | 
It takes only around 9 seconds to find the all Ramanujan primes less than 109 on modern computers:
| In[2]:= | ![rp = NumericArray[ResourceFunction["RamanujanPrimes"][24491666], "Integer32"]; // AbsoluteTiming](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/2358c48cd9a98798.png) | 
| Out[2]= |  | 
The largest Ramanujan prime just under 109:
| In[3]:= | ![rp[[-1]]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/401fd3a8f6a4d05f.png) | 
| Out[3]= |  | 
The number of Ramanujan primes less than one billion:
| In[4]:= | ![Length[rp]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/2f7cc347483e888c.png) | 
| Out[4]= |  | 
Compute the same number using the definition of Ramanujan primes:
| In[5]:= | ![PrimePi[rp[[-1]]] - PrimePi[rp[[-1]]/2]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/061d563b4fa583ef.png) | 
| Out[5]= |  | 
Recover the sequence of natural numbers from Ramanujan primes:
| In[6]:= | ![rp = ResourceFunction["RamanujanPrimes"][20]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/1d9b687708ead10f.png) | 
| Out[6]= |  | 
| In[7]:= | ![PrimePi[#] - PrimePi[#/2] & /@ rp](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/08808f995c03b1d6.png) | 
| Out[7]= |  | 
For all n>=1, we have the following bounds for the n-th Ramanujan prime:
| In[8]:= | ![rp = ResourceFunction["RamanujanPrimes"][200];](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/7ad44fd4eb92b2a2.png) | 
| In[9]:= | ![DiscretePlot[{2*n*Log[2*n], rp[[n]], 41/47*Prime[3*n]}, {n, 200}, Filling -> None, PlotLegends -> {2*n*Log[2*n], "nth Ramanujan Prime", Row[{41/47, Prime[3 n]}]}]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/427b768fe1291926.png) | 
| Out[9]= |  | 
Use "Ramanujan Primes" on Wolfram Demonstrations to visualize the patterns for small Ramanujan primes:
| In[10]:= | ![ResourceData["Demonstration: Ramanujan Primes"]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/3660d8daa3fa3cbb.png) | 
| Out[10]= |  | 
The input must be positive integer. Otherwise it returns unevaluated:
| In[11]:= | ![ResourceFunction["RamanujanPrimes"][-10]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/53a424d4ee01310e.png) | 
| Out[11]= |  | 
If pn+2 = pn+1, then pn and pn+1 are twin primes. If Rn+2 = Rn+1, then Rn and Rn+1 are twin Ramanujan primes; the smallest are 149 and 151. Find the number twin Ramanujan primes in the first 100k Ramanujan primes:
| In[12]:= | ![rp = ResourceFunction["RamanujanPrimes"][10^6];](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/3e2ea0cc11b72941.png) | 
| In[13]:= |  | 
The distribution of gaps between Ramanujan primes:
| In[14]:= | ![Histogram[gap, {1}, Ticks -> {{ 2, Splice[Range[20, 100, 20]]}, Automatic}]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/16b74edbd597848d.png) | 
| Out[14]= |  | 
The number of pairs of twin Ramanujan primes:
| In[15]:= | ![Count[gap, 2]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/35aa3dd98cf8f065.png) | 
| Out[15]= |  | 
See the twin Ramanujan primes:
| In[16]:= | ![Short[trp = rp[[{#, # + 1}]] & /@ Flatten[Position[gap, 2]], 3]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/723edd85758cf322.png) | 
| Out[16]= |  | 
Import a definition of Brun's constant. This is an analogous topic:
| In[17]:= | ![Style[Entity["MathWorld", "BrunsConstant"][
  EntityProperty["MathWorld", "TypesetDescription"]], FontFamily -> "Helvetica"]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/38823a7b709c5ec8.png) | 
| Out[17]= |  | 
Here only twin Ramanujan primes are applied:
| In[18]:= | ![Total[1/N[Flatten[trp], 50]]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/55d7eb4f73cabdd8.png) | 
| Out[18]= |  | 
The number of twin Ramanujan prime pairs less than 10, 100, 1000,…, 109:
| In[19]:= | ![res = ResourceFunction["RamanujanPrimes"][24491666];
loc = ResourceFunction["BinarySearch"][res, #] & /@ (10^Range[3, 9]);
ct = Count[Differences[res[[;; # - 1]]], 2] & /@ loc;](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/25ce6a28f670d2f5.png) | 
| In[20]:= | ![ResourceFunction["NiceGrid"][
 Transpose@{Range[3, 9], ct}, {"\!\(\*SuperscriptBox[\(10\), \(n\)]\)",
   "# of Twin RP"}, Alignment -> Right]](https://www.wolframcloud.com/obj/resourcesystem/images/e41/e41e9af1-6299-4d94-ab54-cd33d06fded2/0148b047fa42990e.png) | 
| Out[20]= |  | 
Wolfram Language 14.0 (January 2024) or above
This work is licensed under a Creative Commons Attribution 4.0 International License