Once we have the boolean list A set up, we can do various counts. The simplest is just to count the number of primes between 1 and n. This is accomplished by counting how many of the entries of A are equal to True.
The following function does that:
def nprimes():
count = 0 for b in A:
if b: count+=1 return count
You may not even want a function here. Just use the middle three lines and then print count.
Twin primes are two primes that di¤er by 2, like 17 and 19, or 71 and 73. While we have known since Euclid that there are an in…nite number of primes, no one has succeeded in proving that there are an in…nite number of twin primes. We can count the number of (pairs of) twin primes in the same way we counted the number of primes:
def ntwins(n):
count = 0
for m in range(2,len(A)):
if A[m] and A[m-2]: count+=1 return count
The conditional statement count+=1 is executed exactly when both A[m]
and A[m-2] are True, that is, when m 2; m is a pair of twin primes.
The gap between consecutive primes p and q is q p 1. The gap between 2 and 3 is zero (there are no nonprimes between 2 and 3) while the gap between 7 and 11 is three (there are three nonprimes, 8; 9; 10, between 7 and 11). The gap between twin primes is 1.
Our next task is to count the number of gaps of di¤erent sizes between consecutive primes from 1 to n. Our result should be an array G such that G [i] is the number of gaps of size i. It will be convenient to focus on the larger of the two consecutive primes. So our main loop will run i from 3 to n. But we have to compare i with the value of the previous prime, so we will need a variable to take care of that: say lastprime. Since we are starting at i = 3, we initially set the value of lastprime equal to 2. So our function will contain the lines:
def gaps(n):
lastprime = 2 for i in range(3,n-1):
return G
Of course we have to set up G as a list at the beginning, and we will only do something in the loop when i is prime. Also, when we …nd the next prime i, and we do something in the loop, we have to set lastprime equal to i before we go looking for the next prime after i. So our function will contain the lines:
def gaps(n):
G = []
lastprime = 2 for i in range(3,n-1):
if(A[i]):
lastprime = i return G
Now what do we want to do with the numbers lastprime and the next prime after it, which will be i? The gap g between those two consecutive primes is i - lastprime - 1. We want to increase G[g] by 1. A problem here is that G[g] may not be de…ned because this may be the …rst gap of size g or more that we have encountered. So we test to see if G[g] is de…ned. If it isn’t, we append zeros to G until it is. So our function becomes:
def gaps(n):
G = []
lastprime = 2
for i in range(3,n+1):
if A[i]:
g = i - lastprime - 1
while g >= len(G): G.append(0) G[g]+=1
lastprime = i return G
A prime triple (or triplet) is a sequence of three primes p0 < p1 < p2 such that p2 = p0+ 6. Here are the …rst six examples:
(5; 7; 11); (7; 11; 13); (11; 13; 17); (13; 17; 19); (17; 19; 23); (37; 41; 43) You can see a list of the …rst elements of the prime triples of the form (p; p + 2; p + 6), like (5; 7; 11), from 5 to 5477 at oeis.org/A022004, and one of the …rst elements of the prime triples of the form (p; p + 4; p + 6), like (7; 11; 13), from 7 to 5437, at oeis.org/A022005.
Exercise 1. Write a program that counts the number of prime triples of the form (p; p + 2; p + 6) in the …rst two thousand numbers. Compare your results with those of Thomas R. Nicely on his site at http://www.trnicely.net/
triplets/t3a_0000.htm.
5 Mersenne primes
A Mersenne prime is a prime of the form 2n 1. So 3 = 22 1is a Mersenne prime, as are 7 = 23 1 and 31 = 25 1. Of course 15 = 24 1 is not a prime at all, let alone a Mersenne prime. In fact it is not di¢ cult to show that if 2n 1is a prime, then so is n. Essentially the argument is
2ab 1 = (2a 1) 2a(b 1)+ 2a(b 2)+ + 2a+ 1
if you can call that an argument. It shows that 2a 1 is a nontrivial factor of 2ab 1 if a is a nontrivial factor of ab.
So the question is, for what primes p is 2p 1a prime. We denote 2p 1by Mp, in honor of Mersenne. We have seen that M2, M3, and M5 are primes.
What about M7 = 27 1 = 127. Yes, it’s also prime. Marin Mersenne said, in 1644, that for the primes p 157, the ones for which Mp is prime are 2; 3; 5; 7; 13; 17; 19; 31; 67; 127; 157. In 1750, Euler showed that M31was a prime. In 1883, Pervouchine showed that M61 is prime, so Mersenne missed one.
Mersenne primes are interesting because, among other things, they are tied up with perfect numbers. Euclid talked about perfect numbers, and showed that an even number is perfect exactly when it is of the form (2n 1) 2n 1, and 2n 1 is prime. So we get a perfect number for n = 2; 3; 5; 7; 13; : : :.
These numbers are 6, 28, 496, 8128, 33 550 336, . . . . The …rst four of these numbers have been known since antiquity. St. Augustine made a big deal out of the fact that 6 was a perfect number, saying that God created the world in six days because of this.
No one knows whether or not there are any odd perfect numbers.
Write a program that …gures out for which primes p < 60 the number Mp
is a prime. When Mp is not prime, this program should print out its smallest prime factor. However, that there is technical problem with the function spf when applied to very large numbers. Here is the function spf as we de…ned it before:
def spf(n):
for m in range(2,n+1):
if n % m == 0: return m if m*m > n: return n
The problem is that the list, range(2,n+1), is just too big. What we need to do is to de…ne the function spf without forming that list. For this purpose we use a while-loop instead of a for-loop. The …rst three lines would be
def spf(n):
m = 2
while m*m <= n:
First we set m equal to 2, then we run a block of code for as long as m2 does not exceed n. Of course that might be forever, so we have to be careful when writing the block that m2 eventually exceeds n. To do that, we will increase
m by 1 at the end of the block, so that the whole thing acts like a for-loop.
We are looking for the smallest nontrivial factor m of n, so we test to see if m divides n, as before. Here is the whole de…nition:
def spf(n):
m = 2
while m*m <= n:
if n % m == 0: return m m = m+1
return n
Notice there are two things we have to do with a while-loop that were taken care of automatically by the for-loop. We have to de…ne the starting value of m outside the loop, and we have to increase m inside the loop.
5.1 The Lucas-Lehmer test
There’s a really good test that tells you whether or not Mp is prime for a given prime p. It is mainly because of this test that the largest known prime at any given time has always been a Mersenne prime. Lucas himself used it to show that M127 was prime in 1876. This number is 170 141 183 460 469 231 731 687 303 715 884 105 727.
Here’s how Lucas’s test, as modi…ed by Lehmer, goes. To see if Mp is prime, we form a sequence of numbers s0; s1; s2; : : : ; sp 1. Set s0 = 4, and then, for each k > 0, set sk = s2k 1 2 until you get up to sp 2. It turns out that Mp is prime exactly when it divides sp 2. In order to keep the numbers reasonably small, do all the computations sk = s2k 1 2 modulo Mp, rather than waiting until the end to divide some monstrous number by Mp. So Mpis prime, exactly when we end up with sp 2= 0. Using the Lucas-Lehmer test, you should be able to …gure out for which primes p < 1000 the number Mp
is a prime. Do it. That will also tell you what all the even perfect numbers are that are less than . . . what?