Here are a couple of programs for working with primes. The first is not so useful - just returning a list of all primes up to the parameter passed.
The next one you pass a list of knownPrimes and a new upper bound and primes are added onto the list returning an expanded knownPrimes at the end
The final one opens a file, checks that there are no composite numbers included by sorting the list and then checking that none of the later numbers are composites divisible by the earlier numbers. It also checks that the number of primes in the file is in the range that you would expect.
So, we are missing a function to actually write the new list of primes onto disk...
What I learnt
How to open a file on the disk and also a common way to read in line by line. For each line in the file open up the line and then use the strip() method to take out end of line keys and similar out of the string before converting into an integer.
listOfPrimes(n) - primes from 2 to n
def listOfPrimes(n):
"""supply a number, Returns a list of all the prime numbers from 2 up to parameter n"""
myPrimes = [2]
print(myPrimes[0])
isAPrime = None #define is a prime - probably not needed
for i in range(3,n+1): #up to n+1 in order to include n in range
for j in range(len(myPrimes)):
if i % myPrimes[j] == 0:
isAPrime = False
break
else:
isAPrime = True
if(isAPrime):
myPrimes.append(i)
print(myPrimes[j+1])
return myPrimes
boundedListOfPrimes(knownPrimes, n) - adds primes up to n to an existing list of primes
def boundedListOfPrimes(knownPrimes,n):
"""supply a list of known primes and an upper bound
works out the primes between the highest of knownPrimes and the upper bound.
appends the newly found primes to the list
in order to work the knownPrimes list must be complete from 2 onwards. """
if len(knownPrimes) == 0:
knownPrimes[0] = 2 #special case of first time to ever work out list set first value as 2
isAPrime = None #initial declaration of IsAPrime - start assuming isAPrime is true - i.e. numbers are all prime
highestKnownPrime = knownPrimes[len(knownPrimes)-1] #pick last one in the list
print("highest known prime",highestKnownPrime)
for i in range(highestKnownPrime,n+1): #n+1start testing from one more than last no in known primes
for j in range(len(knownPrimes)):
if i % knownPrimes[j] == 0:
isAPrime = False
break
else:
isAPrime = True
if(isAPrime):
knownPrimes.append(i)
#print(j+1,knownPrimes[j+1])
return knownPrimes
checkPrimesFile(primesFileName) - opens a file of primes and carries out some checks, returns the file
def checkPrimesFile(primesFileName):
"""purpose is to return an int list of numbers in a file after doing some checks that there
are no composite numbers in the file
asks for hte name of a primes file then reads in a text file of prime numbers and puts into an array
checks that all numbers in the file are primes by sorting it to the low to high and
then starting from the lowest testing the lowest against all the highest to make sure
nothing missing - then finally checks the theoretical distribution of primes against
the number.
Function has to be passed a null string or a name of a file. If it is a null string then
the function will ask for the name
"""
from math import log
if primesFileName == "":
primesFileName = input("Type the name of the Primes file")
print("Workign on ",primesFileName)
primesFileData = open(primesFileName)
myPrimes=[]
for line in primesFileData:
myPrimes.append(int(str(line).strip())) #make sure an integer and the last number is stripped
primesFileData.close()
myPrimes.sort() #make sure that the lowest number is first
includesAComposite = False #assume that file is valid to start with
for i in range(0,len(myPrimes)): #for every prime in the list
for j in range (i + 1,len(myPrimes)): #after sortign the list go through numbers bigger than i
if myPrimes[j] % myPrimes[i] == 0:
print ("found a composite number divisible by primes in the list - indedx is",j," number is", myPrimes[j])
includesAComposite = True #as we found one
break
if includesAComposite:
print("includes a composite or a duplicate prime so prime frequency data not checked")
else:
highestPrime = max(myPrimes)
lowerBound = highestPrime / log(highestPrime)
upperBound = lowerBound * 1.25506
if (len(myPrimes) > lowerBound and len(myPrimes) < upperBound):
print ("the number of primes " + str(len(myPrimes)) + " is within the bounds of ", lowerBound, upperBound)
else:
print ("out of bounds", len(myPrimes), lowerBound, upperBound)
return myPrimes
No comments:
Post a Comment