-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
237 lines (182 loc) · 6.53 KB
/
main.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
#!/usr/bin/python3
"""This is a simple project for educational purposes.
It is does not deal with fast program algorithms as per complexities and
executions, but it teaches how multiple factors for a number is computed
and also to check if multiple pairs are co-prime
The program operates from 2 modes:
- If from the CLI a filepath is specified with the program name during
execution, it reads from the file a line at a time and try to generate
factors for the numbers
- If no filename is specified, it generates a random number and solves
for the multiple factors and the co-primes.
Raises:
If the program freezes, and the CTRL + Z or CTRL + C keys are pressed,
a KeyboardInterrupt error is generated.
"""
def factorize(val: int) -> list | None:
"""This function gets the multiples of a given number greater than 1
Examples:
>>> print(factorize(4))
[(2, 2)]
>>> print(factorize(15))
[(5, 3), (3, 5)]
Args:
val (int): Value for which to get its factors/multiples
Return:
(None): if `val` is less than 2.
(list): a list of tuples containing the different multiples of `val`
"""
local_factor = []
i_fact = 2
if val < 2:
return
for i in range(2, val + 1):
for j in range(2, val + 1):
if i * j == val:
local_factor.append((j, i))
return local_factor
def is_prime(val: int) -> bool:
"""This function checks if a specified number is a prime number.
Examples:
>>> is_prime(3)
True
>>> is_prime(4)
False
Args:
val (int): The value to be checked if it is prime or not
Return:
(bool): `True` if the number is prime and greater than 1 else `False`
"""
for num in range(2, val):
if val % num == 0:
return False
return val > 1
def get_factors(val: int) -> list:
"""This function checks deeply if the `val` is a prime number only
divisible by 1 and itself.
Examples:
>>> print(get_factors(4))
[(2, 2)]
>>> print(get_factors(7))
[(7, 1)]
>>> print(get_factors(15))
[(5, 3), (3, 5)]
Args:
val (int): The value to be checked if it is prime or not
Return:
(list): if `val` is a prime, it returns a list of tuple (1 and itself)
as the only multiples of `val` else it returns a list of tuples with
all factors/multiples
"""
if is_prime(val):
factor = val, 1
return [(factor)]
local_factor = factorize(val)
return local_factor
def factors_are_prime(val: list) -> list | None:
"""This function accepts a list of tuples and check each pair of tuple if
each element are co-prime
Examples:
>>> print(factors_are_prime([(2, 2)]))
[(2, 2)]
>>> print(factors_are_prime([(7, 1)]))
[(7, 1)]
>>> print(factors_are_prime([(5, 3), (3, 5)]))
[(5, 3)]
Args:
val (list): A list containing the multiples or factors of a given num.
Return:
(list): a list with a single tuple if the length of the tuple is 1
if the length is not one, it scans through the list tuple by
tuple to check for a pair that are co-prime.
(None): if no co-prime is found
"""
if len(val) == 1:
return val
for i in val:
if is_prime(i[0]) and is_prime(i[1]):
return i
return
def read_from_file(file_name: str) -> None:
"""This function accepts a filename as an argument, read through each line
and uses the value as the number to factorize
Examples:
>>> read_from_file("tests/test00")
Generated Number: 623:
The list of factor(s):
[(89, 7), (7, 89)]
623 has prime multiple factors: (89, 7)
>>> read_from_file("tests/tes")
[Errno 2] No such file or directory: 'tests/tes'
# if the value read is a string
>>> read_from_file("tests/tes")
invalid literal for int() with base 10: 'value'
Args:
file_name (str): A filename specifying the full path to the file to read
from.
Raises:
(FileNotFoundError): if the file specified is not found.
(ValueError): When the value read from the file is not valid
(TypeError): If the value read is not of type integer
Return:
(None)
"""
try:
with open(file_name) as file:
line = file.readline()
while line != "":
number = int(line.split('\n')[0])
factors = get_factors(number)
prime_mult_fact = factors_are_prime(factors)
print(f"Generated Number: {number}: ")
print(f"The list of factor(s):\n{factors}")
if prime_mult_fact is None:
print(f"{number} has no multiple factors that are both prime.\n")
else:
print(f"{number} has prime multiple factors: {prime_mult_fact}\n")
line = file.readline()
except (FileNotFoundError, ValueError, TypeError) as err:
print(err)
def use_random() -> None:
"""This function generates a random number for the RSA factor
Examples:
>>> use_random()
Generated Number: 571:
The list of factor(s):
[(571, 1)]
571 has prime multiple factors: [(571, 1)]
>>> use_random()
Generated Number: 551:
The list of factor(s):
[(29, 19), (19, 29)]
551 has prime multiple factors: (29, 19)
>>> use_random()
Generated Number: 290:
The list of factor(s):
[(145, 2), (58, 5), (29, 10), (10, 29), (5, 58), (2, 145)]
290 has no multiple factors that are both prime.
Return:
(None):
"""
lower_range = r.randint(1, 10)
upper_range = r.randint(100, 1000)
number_gen = r.randint(lower_range, upper_range)
factors = get_factors(number_gen)
prime_mult_fact = factors_are_prime(factors)
print(f"Generated Number: {number_gen}: ")
print(f"The list of factor(s):\n{factors}")
if prime_mult_fact is None:
print(f"{number_gen} has no multiple factors that are both prime.\n")
else:
print(f"{number_gen} has prime multiple factors: {prime_mult_fact}\n")
if __name__ == "__main__":
from sys import argv
import random as r
try:
if len(argv) == 2:
filename = argv[1]
read_from_file(filename)
else:
use_random()
except KeyboardInterrupt:
print("Program was interrupted.")