-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj1.py
274 lines (190 loc) · 6.35 KB
/
proj1.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#File: proj1.py
#Author: Innocent Kironji
#Date: 03/16/2017
#Section: CMSC201-22
#E-mail: wambugu1@umbc.edu
#Description:
# Asks the user to define a range of numbers between a set
# maximum and minumun. Then the code prints out information
# about the numbers within the user's range
#Maximum and minimum number allowed to recieve from user
MIN_NUM = 1
MAX_NUM = 100000
#Return avlues for checkForPerfect function
PERF = "Perfect"
ABD = "Abundant"
DEF = "Deficient"
#Prints greeting that explains purpose of the program and what the user
#will be doing
def printGreeting():
#Prints the following greeting
print("This program classifies positive integers as Odd/Even, Prime/Composite, Perfect/Abundant/Deficient, Square, and Triangular")
print()
print("You will now get to choose the range of positive integers that you would like to see classified.")
#Prints the heading of the table
def printTableHead():
#Prints the following heading
print("Int", "\t Classifications......................................")
print("---------------------------------------------------------------")
#Prints the information for one number on one line of the table
def printTableLine(num, odd, prime, perf, square, tri):
print(num, end = '')
#isOdd printer
if (odd == True):
print("\t Odd", end = '')
else:
print("\t Even", end = '')
#isPrime printer
if (num == 1):
print("\t Neither", end = '')
elif (prime == True):
print("\t Prime ", end = '')
else:
print("\t Composite", end = '')
#checkForPerfect printer
print("\t", perf, end = '')
#isSquare printer
if (square == True):
print("\t Square", end = '')
else:
print("\t ", end = '')
#isTriangular printer
if (tri == True):
print("\t Triangular")
else:
print("\t ")
#Reprompts the user until they enter a number between a min and max
def getValidInt(minn, maxx):
#Defining variables
message = "Enter a number between " + str(minn) + " and " + str(maxx) + " (inclusive): "
#Priming Read
newInt = int(input(message))
#Checks to make sure user entered a number within the range (inclusive)
while (newInt < minn) or (newInt > maxx):
print("That number is not allowed. Please try again!")
newInt = int(input(message))
return newInt
#Calculates if a number is divisable by another number
def isDivisor(origNum, possDiv):
#Defining variables
numIsDiv = False
#Regular will trail with normal decimals (ex. _.12)
#while even will trail with zeros (ex. _.00)
regQuotient = float(origNum / possDiv)
evenQuotient = float(origNum // possDiv)
#Checks divisability--if both regular and even trail with zeros
#then the number is divisable
if (regQuotient == evenQuotient):
numIsDiv = True
return numIsDiv
#Calculates the sum of the divisors of a number
def sumDivisors(num):
#Defining variables
divisables = []
counter = 1
summ = 0
#Checks if a value is divisable
while (counter < num):
divises = isDivisor(num, counter)
#Add divisable values to a list
if (divises == True):
divisables.append(counter)
#Updates counter
counter += 1
#Resets counter
counter = 0
#Sums the values in the list divisables
while (counter < len(divisables)):
summ += divisables[counter]
#Updates counter
counter += 1
return summ
#Calculates sum of dividers and returns whether that sum is perfect,
#abundant, or deficient
def checkForPerfect(num):
#Defining variables
summ = sumDivisors(num)
#Checks and returns if perfect, abundant or deficient
if (summ == num):
return PERF
elif (summ > num):
return ABD
elif (summ < num):
return DEF
#Calculates whether num is odd or not
def isOdd(num):
#Defining variables
numIsOdd = False
#Checks oddness
if (num % 2 != 0):
numIsOdd = True
return numIsOdd
#Calculates whether num is prime or not
def isPrime(num):
#Defining variables
numIsPrime = False
#A sum of 1 for it's divisors would prove that num is only divisible
#by 1 (and itself)
onlyOne = sumDivisors(num)
#Checks if number is only divisible by one and itself
if (onlyOne == 1):
numIsPrime = True
return numIsPrime
#Calculates whether num is square or not
def isSquare(num):
#Defining variables
numIsSquare = False
currentNum = 1
#Checks squareness of num
while (currentNum <= num):
#Defining another variable
quotient = num / currentNum
#Shows that currentNum times itself equals num (therefore square)
if (currentNum == quotient):
numIsSquare = True
#Updates currentNum counter
currentNum += 1
return numIsSquare
#Calculates whether num is triangular or not
def isTriangular(num):
#Defining variables
numIsTri = False
counter = 0
currentSum = 0
#Checks triangularity of num
while (counter <= num):
#Creates a sum of consecutive integers
currentSum += counter
#During the loop if at any time the sum equals num
#numIsTri becomes and stays true until code ends
if (currentSum == num):
numIsTri = True
#Updates counter
counter += 1
return numIsTri
def main():
#Print the greeting to the user
printGreeting()
#Get the number range from the user
print("Start with which positive integer?")
minn = getValidInt(MIN_NUM, MAX_NUM)
print("End with which positive integer?")
maxx = getValidInt(minn, MAX_NUM)
#Before we start printing the number info, print the table head
printTableHead()
#Defining variables
counter = minn
end = maxx + 1
#Print out all of the numbers and their properties
while (counter < end):
#set parameters for printTableLine function
odd = isOdd(counter)
prime = isPrime(counter)
perf = checkForPerfect(counter)
square = isSquare(counter)
tri = isTriangular(counter)
#prints out the properties
printTableLine(counter, odd, prime, perf, square, tri)
#Updates counter
counter += 1
main()