Function Repository Resource:

TwinPrimes

Source Notebook

Get the pairs of primes that differ by 2

Contributed by: Wolfram Staff

ResourceFunction["TwinPrimes"][n]

gives the first n pairs of primes that differ by 2.

Examples

Basic Examples (3) 

Here are the first 10 pairs of twin primes:

In[1]:=
ResourceFunction["TwinPrimes"][10]
Out[1]=

Indeed, the differences are 2:

In[2]:=
Differences /@ %
Out[2]=

For reference, here are the first 30 primes:

In[3]:=
Prime[Range[30]]
Out[3]=

Neat Examples (3) 

The sum of the reciprocals of the primes diverges:

In[4]:=
Sum[1/Prime@n, {n, \[Infinity]}]
Out[4]=

In 1919, Brun proved that the sum of the reciprocals of the twin primes converges. The sum, Brun's constant, is around 1.9. Here is the sum of the reciprocals of the first 100,000 twin primes:

In[5]:=
Total[1./Flatten@ResourceFunction["TwinPrimes"][10^5]]
Out[5]=

This is the last pair in that sum:

In[6]:=
Last@ResourceFunction["TwinPrimes"][10^5]
Out[6]=

Version History

  • 2.0.0 – 05 July 2019
  • 1.0.0 – 25 February 2019

Related Resources

License Information