project euler problem 27 https projecteuler netproblem27 problem statement euler discovered the remarkable quadratic formula n2 n 41 it turns out that the formula will produce 40 primes for the consecutive values n 0 to 39 however when n 40 402 40 41 4040 1 41 is divisible by 41 and certainly when n 41 412 41 41 is clearly divisible by 41 the incredible formula n2 79n 1601 was discovered which produces 80 primes for the consecutive values n 0 to 79 the product of the coefficients 79 and 1601 is 126479 considering quadratics of the form n an b where a lt 1000 and b lt 1000 where n is the modulusabsolute value of ne g 11 11 and 4 4 find the product of the coefficients a and b for the quadratic expression that produces the maximum number of primes for consecutive values of n starting with n 0 checks to see if a number is a prime in osqrtn a number is prime if it has exactly two factors 1 and itself returns boolean representing primality of given number num i e if the result is true then the number is indeed prime else it is not isprime2 true isprime3 true isprime27 false isprime2999 true isprime0 false isprime1 false isprime10 false 2 and 3 are primes negatives 0 1 all even numbers all multiples of 3 are not primes all primes number are in format of 6k 1 solution1000 1000 59231 solution200 1000 59231 solution200 200 4925 solution1000 1000 0 solution1000 1000 0 checks to see if a number is a prime in o sqrt n a number is prime if it has exactly two factors 1 and itself returns boolean representing primality of given number num i e if the result is true then the number is indeed prime else it is not is_prime 2 true is_prime 3 true is_prime 27 false is_prime 2999 true is_prime 0 false is_prime 1 false is_prime 10 false 2 and 3 are primes negatives 0 1 all even numbers all multiples of 3 are not primes all primes number are in format of 6k 1 solution 1000 1000 59231 solution 200 1000 59231 solution 200 200 4925 solution 1000 1000 0 solution 1000 1000 0 length a b
import math


def is_prime(number: int) -> bool:
    

    if 1 < number < 4:
        
        return True
    elif number < 2 or number % 2 == 0 or number % 3 == 0:
        
        return False

    
    for i in range(5, int(math.sqrt(number) + 1), 6):
        if number % i == 0 or number % (i + 2) == 0:
            return False
    return True


def solution(a_limit: int = 1000, b_limit: int = 1000) -> int:
    
    longest = [0, 0, 0]  
    for a in range((a_limit * -1) + 1, a_limit):
        for b in range(2, b_limit):
            if is_prime(b):
                count = 0
                n = 0
                while is_prime((n**2) + (a * n) + b):
                    count += 1
                    n += 1
                if count > longest[0]:
                    longest = [count, a, b]
    ans = longest[1] * longest[2]
    return ans


if __name__ == "__main__":
    print(solution(1000, 1000))