From 290e4b2cb06de4b2a8dc161a176148cec9db3cdd Mon Sep 17 00:00:00 2001 From: steven-y-e Date: Tue, 1 Aug 2023 16:32:11 -0400 Subject: [PATCH] feat(solution): problem #27 --- solutions/027/quadraticprimes.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solutions/027/quadraticprimes.py diff --git a/solutions/027/quadraticprimes.py b/solutions/027/quadraticprimes.py new file mode 100644 index 0000000..c691bff --- /dev/null +++ b/solutions/027/quadraticprimes.py @@ -0,0 +1,29 @@ + +import math + +def is_prime(maybeprime): + for i in range(2, math.floor(math.sqrt((abs(maybeprime))) + 1)): + if maybeprime % i == 0: + return False + return True + +def consecutive_primes_in_quadratic_formula(a, b): + n = 0 + while(True): + fx = n * n + a * n + b + if is_prime(fx): + n += 1 + else: + return n + +mostConsecutive = 0 +mostConsecutiveAB = 0 + +for a in range(-999, 999): + for b in range(-1000, 1000): + consecutivePrimes = consecutive_primes_in_quadratic_formula(a, b) + if consecutivePrimes > mostConsecutive: + mostConsecutive = consecutivePrimes + mostConsecutiveAB = a * b + +print("The product of the a & b that produce the most consecutive primes as coefficients in a quadratic formula is: ", mostConsecutiveAB)