|
| 1 | +import concurrent.futures |
| 2 | +import multiprocessing |
| 3 | +import queue |
| 4 | +import itertools |
| 5 | +import signal |
| 6 | +import time |
| 7 | + |
| 8 | +BOUND = 10**5 |
| 9 | + |
| 10 | +in_queue = multiprocessing.Queue(100) |
| 11 | +exit_event = multiprocessing.Event() |
| 12 | + |
| 13 | + |
| 14 | +def exit_handler(signum, frame): |
| 15 | + exit_event.set() |
| 16 | + |
| 17 | + |
| 18 | +signal.signal(signal.SIGINT, exit_handler) |
| 19 | +signal.signal(signal.SIGTERM, exit_handler) |
| 20 | + |
| 21 | + |
| 22 | +def collatz(n): |
| 23 | + steps = 0 |
| 24 | + while n > 1: |
| 25 | + if n % 2: |
| 26 | + n = n * 3 + 1 |
| 27 | + else: |
| 28 | + n = n // 2 |
| 29 | + steps += 1 |
| 30 | + return steps |
| 31 | + |
| 32 | + |
| 33 | +def collatz_consumer(target): |
| 34 | + count = 0 |
| 35 | + while True: |
| 36 | + if not in_queue.empty(): |
| 37 | + try: |
| 38 | + n = in_queue.get(timeout=1) |
| 39 | + except queue.Empty: |
| 40 | + return count |
| 41 | + |
| 42 | + if collatz(n) == target: |
| 43 | + count += 1 |
| 44 | + |
| 45 | + if exit_event.is_set(): |
| 46 | + return count |
| 47 | + |
| 48 | + |
| 49 | +def range_producer(): |
| 50 | + for n in range(2, BOUND): |
| 51 | + if exit_event.is_set(): |
| 52 | + return |
| 53 | + try: |
| 54 | + in_queue.put(n, timeout=1) |
| 55 | + except queue.Full: |
| 56 | + exit_event.set() |
| 57 | + return |
| 58 | + |
| 59 | + while True: |
| 60 | + time.sleep(0.05) |
| 61 | + if in_queue.empty(): |
| 62 | + exit_event.set() |
| 63 | + return |
| 64 | + |
| 65 | + |
| 66 | +def length_counter(target): |
| 67 | + with concurrent.futures.ProcessPoolExecutor() as executor: |
| 68 | + executor.submit(range_producer) |
| 69 | + results = executor.map( |
| 70 | + collatz_consumer, |
| 71 | + itertools.repeat(target, 4) |
| 72 | + ) |
| 73 | + |
| 74 | + return sum(results) |
| 75 | + |
| 76 | + |
| 77 | +def get_input(prompt): |
| 78 | + while True: |
| 79 | + n = input(prompt) |
| 80 | + try: |
| 81 | + n = int(n) |
| 82 | + except ValueError: |
| 83 | + print("Value must be an integer.") |
| 84 | + continue |
| 85 | + |
| 86 | + if n <= 0: |
| 87 | + print("Value must be positive.") |
| 88 | + else: |
| 89 | + return n |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + print("Collatz Sequence Counter") |
| 94 | + |
| 95 | + target = get_input("Collatz sequence length to search for: ") |
| 96 | + print(f"Searching in range 1-{BOUND}...") |
| 97 | + |
| 98 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 99 | + future_guess = executor.submit( |
| 100 | + get_input, |
| 101 | + "How many times do you think it will appear? " |
| 102 | + ) |
| 103 | + |
| 104 | + count = length_counter(target) |
| 105 | + |
| 106 | + guess = future_guess.result() |
| 107 | + |
| 108 | + if guess == count: |
| 109 | + print("Exactly right! I'm amazed.") |
| 110 | + elif abs(guess - count) < 100: |
| 111 | + print(f"You're close! It was {count}.") |
| 112 | + else: |
| 113 | + print(f"Nope. It was {count}.") |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments