Some ideas for the MATHEMATICA assignment on twin primes
The first cell defines a function (named primegap) which defines the distance
from the nth prime to the (n+1)st prime. Note the use of the underscore "_" to define a function.
In[1]:=
Out[1]=
We may now plug different values into this function to compute prime gaps as desired:
In[2]:=
Out[2]=
Out[3]=
Out[4]=
And look at primes 2007, 2008, 2009 and 2010 to verify that these gaps are correct:
In[5]:=
Out[5]=
Out[6]=
Out[7]=
Out[8]=
Let's call a prime a "4-prime" if the gap to the next prime equals 4. (This is similar to "twin primes" where the gap to the next prime equals 2.) So the above showed that the 2008th prime (which equals 7467) is a 4-prime.
Here's a cell showing how to create a list of the first 100 primes. (Another way to do it is to enter Prime[Range[100]]).
In[9]:=
Out[9]=
In[10]:=
If we give this list a name (here plist)then we can easily refer back to it in future computations. MATHEMATICA has lots of commands for dealing with lists--try looking under "List" in Help and following the link to section 1.8 of the MATHEMATICA book to see some of these commands. One of the most useful is "Length", and we will also use "Intersection", "Delete", "Count","Position" and "Flatten" below.
In[11]:=
Out[11]=
In[12]:=
Out[12]=
Notice that the previous command subtracted 4 from every element in plist. If we intersect this with plist we will obtain a list of all of the 4-primes in the list of the first 100 primes:
In[13]:=
Out[13]=
Well in fact this was not quite correct. Notice that our routine actually determined those primes p for which p+4 is also prime.Since 3,5 and 7 are primes,3 is in this list but it's not really a 4-prime--however this is the only discrepancy as you can readily prove. Here's a fix:
In[14]:=
Out[14]=
In[15]:=
Out[15]=
So we see that 26 of the first 100 primes are 4-primes. The percentage of 4-primes in the first 100 primes is 26/100.
Here's a different way to generate the same list of 4-primes (which is probably better)
In[16]:=
Out[16]=
In[17]:=
Out[17]=
In[18]:=
Out[18]=
In[19]:=
Out[19]=
In[20]:=
Out[20]=
Now suppose you wanted to list all of the 4-primes up to 1000. First we need to know how many primes are less than 1000. That's the use of PrimePi:
In[21]:=
Out[21]=
So now we can just use our first approach (or the second if you prefer)replacing 100 with 168:
In[22]:=
Out[22]=
In[34]:=
Out[35]=
In[37]:=
Out[37]=
If you want to pick out a particular entry in the list use double brackets"[[ ]]", and if you want to find the maximum or minimum in a list use "Max" and "Min". Using this we can create a list of the gaps between
the 4-primes up to 1000 and pick out the maximum and minimum gaps:
In[40]:=
Out[40]=
In[41]:=
Out[41]=
In[42]:=
Out[42]=
| Created by Mathematica (February 23, 2007) |