Hellooo, im having trouble doing my python project for college.
So basically, i must get the biggest prime number within a certain time and with a certain number of processes.
The teacher gave us this functions, and told as that we can and should modify them:
def find_max_prime(timeout):
"""Finds the biggest prime until timeout."""
max_prime = i = 1
start = time.time()
while time.time() - start < timeout:
if is_prime(i) and i > max_prime:
max_prime = i
i += 1
print(timeout, max_prime)
def is_prime(n):
"""Check if n is prime."""
for i in range(2, n-1):
if n % i == 0:
return False
return True
I tried to work it out with the multiprocessing.Pool() and Manager() but Im still confused, how can I make a certain number of processes program the same function within a time and give me a biggest number of all processes.
So for example, if I have 4 processes running this function "find_max_prime", all of them are gonna start at different numbers and whichever ends first the max prime first is gonna change a shared variable, and then starting coding the function again but after the highest prime found, so something like this,i would say:
def find_max_prime(timeout, number):
"""Finds the biggest prime until timeout."""
max_prime = i = number
start = time.time()
while time.time() - start < timeout:
if is_prime(i) and i > max_prime:
max_prime = i
i += 1
print(timeout, max_prime)
I also tried using Lock() within this method but its my first time using it and im also having trouble with it:
def find_max_prime(timeout, number, lock):
"""Finds the biggest prime until timeout."""
max_prime = i = number
start = time.time()
while time.time() - start < timeout:
with lock:
if is_prime(i) and i > max_prime:
max_prime = i
i += 1
print(timeout, max_prime)