Skip to content
Snippets Groups Projects
Commit 8a63727b authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

real hard work

parent 23237951
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ from manager import QueueClient
class Master(QueueClient):
def run(self):
while True:
task = random.randint(5, 1e3)
task = random.randint(5, 1e4)
print('new task:', task)
self.queue.put(task)
print('tasks left:', self.queue.qsize())
......
......@@ -4,15 +4,17 @@ import random
import time
from manager import QueueClient
from work import find_prime
class Minion(QueueClient):
def run(self):
while True:
task = self.queue.get()
print('start work on task', task, '...')
time.sleep(random.randint(5, 15)) # insert hard work here
print('work done for task', task)
start = time.perf_counter()
print(f'start work on task {task}...')
result = find_prime(task)
print(f'Done ! The {task}-th prime number is {result} (found in {time.perf_counter() - start:.3f}s)')
if __name__ == '__main__':
......
#!/usr/bin/env python3
"""Hard work here. Not efficient, but hard."""
def is_prime(number: int) -> bool:
"""Check if a number is prime."""
return not any(number % i == 0 for i in range(2, number))
def find_prime(goal: int = 10, verbose: bool = False) -> int:
"""Find the goal-th prime number."""
found = 0
i = 1 # let's enumerate all numbers
while found < goal: # until we have enough prime ones.
i += 1
if is_prime(i):
found += 1
if verbose:
print('the prime number n°', found, 'is', i)
return i
if __name__ == '__main__':
print(find_prime(50, verbose=True))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment