#!/usr/bin/env python3 """ Solve the HTB "Primed4Action" coding challenge. Given a whitespace-separated list of integers, the script identifies the two prime numbers in the list and returns their product. """ def is_prime(n: int) -> bool: """Return True if n is prime (simple trial division).""" match n: case _ if n < 2: return False case 2: return True case _ if n % 2 == 0: return False case _: i = 3 while i * i <= n: if n % i == 0: return False i += 2 return True def cal_prime(n): """Return True if n is prime (trial division, sqrt bound).""" if n < 2: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True def main(sended: str) -> int: """Parse input, find the two primes, and return their product.""" # First solution. primes = [ x for x in [int(i) for i in sended.split()] if is_prime(x) ] # x % 2 != 0 or x == 2] result = primes[0] * primes[1] # Second solution. numbers = list(map(int, sended.split())) x, y = [num for num in numbers if cal_prime(num)] print(f"First: {result}, Second: {x*y}") return result if __name__ == "__main__": main( "4559 1021 4907 4536 4367 4500 1633 3261 4754 1939 2487 1840 3272 192 4048 4754 2438 249 147 493 1265 2318 1563 548 1816 565 4387 4081 4747 3828 1464 3971 980 462 2798 3025 4464 244 510 440 1366 1522 2053 3081 4258 46 3110 4945 1457 4811" )