-
Notifications
You must be signed in to change notification settings - Fork 1
/
prime_number_generator.py
70 lines (58 loc) · 1.98 KB
/
prime_number_generator.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
import sys
import time
primes = []
def calculate_prime_numbers(how_many, iterations):
elapsed_time_sum = 0
for i in range(iterations):
start = time.time()
primes = [2]
possible_prime = 3
while len(primes) <= how_many:
is_prime = True
for num in range(2, possible_prime):
if possible_prime % num == 0:
is_prime = False
if is_prime:
primes.append(possible_prime)
possible_prime += 2
end = time.time()
elapsed_time = round(end-start, 4)
elapsed_time_sum += elapsed_time
print('Iteration: {}\nElapsed time: {} seconds'.format(i+1, elapsed_time))
# print(primes[0:-1])
return(elapsed_time_sum/iterations)
def print_help():
print('Hi, this script is a prime number generators/benchmark')
print('The first argument is the number of prime numbers to try, the second number is the number of iterations.')
print("You can use the following command line: 'python prime_number_generator 1000 2' or just 'python prime_number_generator' without arguments.")
sys.exit()
if len(sys.argv) == 1:
try:
how_many = int(input("How many numbers to generate: "))
iterations = int(input("How many iterations: "))
except:
print("Invalid input!")
sys.exit(1)
if len(sys.argv) == 2:
arg1 = sys.argv[1]
if arg1 == 'help' or arg1 == '?':
print_help()
else:
try:
int(arg1) > 0
how_many = int(arg1)
iterations = 1
except:
print("Invalid input!")
sys.exit()
if len(sys.argv) == 3:
arg1 = sys.argv[1]
arg2 = sys.argv[2]
try:
how_many = int(arg1)
iterations = int(arg2)
except:
print("Invalid input!")
sys.exit()
average_time_elapsed = round(calculate_prime_numbers(how_many, iterations), 4)
print("\nAverage time elapsed: {} seconds".format(average_time_elapsed))