Function Repository Resource:

PrimesBetween

Source Notebook

Make a list of all primes within a range

Contributed by: Ted Ersek

ResourceFunction["PrimesBetween"][min,max]

makes a list of all prime numbers between min and max.

Details and Options

The following option can be given:
MethodAutomaticmethod to use
Possible settings for the Method option are Automatic, "Eratosthenes", and "NextPrime".
ResourceFunction["PrimesBetween"] treats all negative integers as not prime.
PrimesBetween will return min and/or max if they are prime.

Examples

Basic Examples (1) 

Make the list of prime numbers between 50 and 300:

In[1]:=
ResourceFunction["PrimesBetween"][50, 300]
Out[1]=

Scope (1) 

The arguments of PrimesBetween can be any real values:

In[2]:=
ResourceFunction["PrimesBetween"][6 \[Pi], 70.4]
Out[2]=

Options (4) 

The default method is Automatic and it tries to use the fastest algorithm for the arguments provided:

In[3]:=
Options[
ResourceFunction["PrimesBetween"]]
Out[3]=

In this case the Eratosthenes sieve method is automatically selected since it's fastest:

In[4]:=
ResourceFunction["PrimesBetween"][1, 10000000]; // AbsoluteTiming // First
Out[4]=

When finding a short list of large primes, nested NextPrime is automatically used since it's fastest:

In[5]:=
ResourceFunction["PrimesBetween"][10^30, 10^30 + 3000]; // AbsoluteTiming // First
Out[5]=

Eratosthenes sieve is often fastest, but it requires more memory than the NextPrime method:

In[6]:=
ResourceFunction["PrimesBetween"][1, 1000000, Method -> "Eratosthenes"]; // MaxMemoryUsed
Out[6]=
In[7]:=
ResourceFunction["PrimesBetween"][1, 1000000, Method -> "NextPrime"]; // MaxMemoryUsed
Out[7]=

Applications (2) 

Find a pattern in the distribution of gaps between prime numbers:

In[8]:=
list = ResourceFunction["PrimesBetween"][1, 5*10^8];
primeGaps = Tally[Differences[Rest@list]];
ListLogPlot[primeGaps]
Out[9]=

Make a list of all seven digit primes. There are over 5,000,000 of them:

In[10]:=
list = ResourceFunction["PrimesBetween"][10^7, 10^8];
Length[list]
Out[11]=

The most common gap between primes is 6 and there are 668,765 such prime gaps:

In[12]:=
primeGaps = Tally[Differences[Rest@list]];
MaximalBy[primeGaps, Last]
Out[13]=

There are 381,332 prime pairs separated by 2. Based on that, one might then suspect there are infinitely many primes that differ by 2. However, that is a famous conjecture that hasn't been proven true:

In[14]:=
Cases[primeGaps, {2, _}]
Out[14]=

Properties and Relations (4) 

The list returned by PrimesBetween includes the arguments when they are prime:

In[15]:=
ResourceFunction["PrimesBetween"][17, 53]
Out[15]=

PrimesBetween considers all negative integers as not prime:

In[16]:=
ResourceFunction["PrimesBetween"][-30, 43]
Out[16]=

PrimesBetween[n1,n2] is only defined when n1n2:

In[17]:=
ResourceFunction["PrimesBetween"][50, 20]
Out[17]=

PrimesBetween uses Eratosthenes sieve to quickly make a list of many consecutive primes:

In[18]:=
min = 10^10;
max = min + 5*10^6;
(list1 = ResourceFunction["PrimesBetween"][min, max]); // RepeatedTiming // First
Out[19]=

It take much longer to make the same list of primes using Table, Prime or by nesting NextPrime:

In[20]:=
(list2 = Table[Prime[k], {k, 1 + PrimePi@min, PrimePi@max}]); // RepeatedTiming // First
Out[20]=
In[21]:=
(list3 = Take[NestWhileList[NextPrime, min, (NextPrime[#] <= max) &], {2 - Boole@PrimeQ@min, -1}]); //
   RepeatedTiming // First
Out[21]=
In[22]:=
list1 === list2 === list3
Out[22]=

Publisher

Ted Ersek

Version History

  • 1.0.0 – 08 October 2025

Related Resources

License Information