def get_primes_till(n): square = lambda n: n * n candiates = range(2, n + 1) primes = [] while candiates: prime = candiates[0] primes.append(prime) candiates = [c for c in candiates if c % prime != 0] return primes def get_coprime(n): primes = get_primes_till(n) for p in primes: if n % p != 0: return p raise Exception("No coprime found for {}.".format(n)) def is_prime_fermat(n): if n == 2: return True a = get_coprime(n) if (a ** (n - 1) % n) != 1: return False else: return True def is_prime_deterministic(n): pass def is_prime(n): if n == 2: return True if n < 2: return False if not is_prime_fermat(n): return False else: return True return is_prime_deterministic(n) def get_length(a, b): def formula(n): return n*n + a*n + b for n in range(99999): if not is_prime(formula(n)): return n def bruteforce(): solution = None options = [(get_length(a, b), a, b) for a in get_primes_till(1000) for b in get_primes_till(1000)] print(max(options)) bruteforce()