Math 2325
Project 4 (Chapter 4) - Prime Numbers

Helmut Knaust, Department of Mathematical Sciences, UTEP, El Paso TX 79968
hknaust@utep.edu
03/13/2021

The Programs

PrimeTest

PrimeQ[k] answers the question: Is k a prime number?

PrimeQ[5]

PrimeQ[6]

ListPrimes

The command below generates a list of all primes less than or equal to n

ListPrimes[n_]:=Module[{k=1,list={}},While[Prime[k]≤n,list=Append[list,Prime[k]];k++];list]

ListPrimes[10]

Count Primes

The nest command gives the number of prime numbers less than or equal to n

PrimePi[5]

MersenneCheck

This command checks whether (2^n)-1 is prime:

MersenneCheck[n_]:=Print[2^n-1, " is prime: ",PrimeQ[2^n-1]]

MersenneCheck[6]

MersenneCheck[607]

While many mathematicians believe there are infinitely many Mersenne primes, there are only 51 known Mersenne primes as of March 11, 2021. 
See: Great Internet Mersenne Prime Search

EulerCheck

This command checks for which m the number m^2+m+41 is prime (for m=0,...,k).
You can change the polynomial formula #^2+#+41. Just think of # as the variable of the polynomial. (Sorry, this is geeky.)

EulerCheck[k_]:=Select[Table[m,{m,0,k}],(PrimeQ[#^2+#+41]&)]

EulerCheck[20]

Count Primes modulo 4

There are no primes divisible by 4 and only one that leaves remainder 2 when divided by 4. The two commands below count how many primes less than or equal to n leave remainder 1, and 3, respectively, upon division by 4.

Pi1[n_]:=Module[{k=1,list={}},While[Prime[k]≤n,list=Append[list,Prime[k]];k++];Select[list,(Mod[#,4]==1&)]//Length]

Pi3[n_]:=Module[{k=1,list={}},While[Prime[k]≤n,list=Append[list,Prime[k]];k++];Select[list,(Mod[#,4]==3&)]//Length]

Pi1[111]

Pi3[111]

Count Primes in an Interval

The last command computes the number of primes in the interval [k,n]:

PrimePiInterval[k_,n_]:=PrimePi[n]-PrimePi[k]+If[PrimeQ[k],1,0]

PrimePiInterval[5,13]