In: Computer Science
Write following program using Python:
A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime.
Write a program that asks the user to enter a integer greater than 1, then displays all of the prime numbers that are less than or equal to the number entered. Last, create two files. One file will hold all of the prime numbers found. The other file will hold all of the composites found.
Your program must maintain two lists. One for prime numbers and another for composite numbers. For every number found to be prime, add it to your prime list. For every number found to be composite, add it to your composite list.
Create a isPrime(n) function. This function accepts a single argument named n. It returns True if n is prime. Conversely, it returns False if n is a composite number. You must use this function to make determinations.
Only after the primes and composites lists are completed, write these results to two files.
For full credit:
Good comments.
Your program should not crash on errors.
Must work for any number n.
Your program maintains 2 lists. One of Primes and one of Composite
numbers.
Your program creates 2 output files. One of Primes and one of
Composite numbers.
Be sure to demonstrate good input validation.
Upload your ORIGINAL work to BlackBoard before the due date and
time. No google, cheg, 'collaborations', or any unoriginal work
will be accepted.
Consider the following for finding prime numbers...
def prime_or_composite(n):
has_divisor = False
for i in range(2, n):
if n % i == 0:
has_divisor = True
if has_divisor:
print(n, 'is composite.')
else:
print(n, 'is prime.')
def isInt(n):
try:
int(n)
return True
except:
return False
def prime_or_composite(n):
has_divisor = False
for i in range(2, n):
if n % i == 0:
has_divisor = True
if has_divisor:
print(n, 'is composite.')
else:
print(n, 'is prime.')
return has_divisor
n = input("Enter a number: ")
if isInt(n):
composites = []
primes = []
for i in range(2, int(n)+1):
if not prime_or_composite(i):
primes.append(str(i))
else:
composites.append(str(i))
f = open("primes.txt", "w")
data = " ".join(primes)
f.write(data)
f.close()
f = open("composites.txt", "w")
data = " ".join(composites)
f.write(data)
f.close()
else:
print("Provided invalid input.")
''' OUTPUT
Enter a number: 100
2 is prime.
3 is prime.
4 is composite.
5 is prime.
6 is composite.
7 is prime.
8 is composite.
9 is composite.
10 is composite.
11 is prime.
12 is composite.
13 is prime.
14 is composite.
15 is composite.
16 is composite.
17 is prime.
18 is composite.
19 is prime.
20 is composite.
21 is composite.
22 is composite.
23 is prime.
24 is composite.
25 is composite.
26 is composite.
27 is composite.
28 is composite.
29 is prime.
30 is composite.
31 is prime.
32 is composite.
33 is composite.
34 is composite.
35 is composite.
36 is composite.
37 is prime.
38 is composite.
39 is composite.
40 is composite.
41 is prime.
42 is composite.
43 is prime.
44 is composite.
45 is composite.
46 is composite.
47 is prime.
48 is composite.
49 is composite.
50 is composite.
51 is composite.
52 is composite.
53 is prime.
54 is composite.
55 is composite.
56 is composite.
57 is composite.
58 is composite.
59 is prime.
60 is composite.
61 is prime.
62 is composite.
63 is composite.
64 is composite.
65 is composite.
66 is composite.
67 is prime.
68 is composite.
69 is composite.
70 is composite.
71 is prime.
72 is composite.
73 is prime.
74 is composite.
75 is composite.
76 is composite.
77 is composite.
78 is composite.
79 is prime.
80 is composite.
81 is composite.
82 is composite.
83 is prime.
84 is composite.
85 is composite.
86 is composite.
87 is composite.
88 is composite.
89 is prime.
90 is composite.
91 is composite.
92 is composite.
93 is composite.
94 is composite.
95 is composite.
96 is composite.
97 is prime.
98 is composite.
99 is composite.
100 is composite.
SAMPLE OUTPUT FILES
primes.txt: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
composites.txt: 4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 26 27 28 30 32 33 34 35 36 38 39 40 42 44 45 46 48 49 50 51 52 54 55 56 57 58 60 62 63 64 65 66 68 69 70 72 74 75 76 77 78 80 81 82 84 85 86 87 88 90 91 92 93 94 95 96 98 99 100
'''
# ----- Up vote or comment. Happy Learning!