-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappendPrimes.py
85 lines (60 loc) · 2.16 KB
/
appendPrimes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
Mason Kam
appendPrimes.py
Created on Feb 24, 2017
Last updated on Mar 6, 2017
Description: This program appends the amount of numbers that is requested to the already existing
list of primes and its category (prime or composite) denoted by 1 or 0. The method used here is the
brute force checking up to the sqrt of the given number to determine whether it is a prime or not.
Instructions: Simply run the program and user input the amount of numbers to append to the given files
under the append definition. Change the file names to change the course.
These are python references to assist in coding notations
-https://docs.python.org/3/tutorial/
-https://www.tutorialspoint.com/python/
-https://docs.python.org/3/library/index.html
'''
import math
def getVal():
while(True):
try:
value = int(input("Please insert num to append to prime list:"))
if(value >= 2):
break
except ValueError:
print("Bad input, try again.") #if it was a string, not an int.
return value
#Check if prime
def testPrime(num):
#Only look up to sqrt of num
for test in range(2,int(math.sqrt(num))+1,1):
if (num % test) == 0:
return False
return True
#Append the amount of numbers specified in the parameter
def append(numToAppend):
#Change these file names to change the files that are appended to
fh = open("primes2.txt",'a')
fh2 = open("numPrimes2.txt",'r+')
fh2.seek(0)
maxNum = int(fh2.readline())
if(numToAppend < 1):
print('invalid number to append')
return
else:
print('Appending: {0}..'.format(numToAppend))
#Test whether next num is prime and append to file
for it in range(maxNum + 1,maxNum + numToAppend + 1,1):
if(testPrime(it)):
fh.write(str(it) + ':' + str(1) + '\n')
else:
fh.write(str(it) + ':' + str(0) + '\n')
#Update Max Number
fh2.seek(0)
fh2.write(str(maxNum + numToAppend))
fh.close()
fh2.close()
print('Done appending')
def main():
num = getVal()
append(num)
if __name__ == '__main__': main()