Computing question: A quadratic which gives primes

117 Views Asked by At

This is about Project Euler Problem 27. The question is:

Considering quadratics of the form $n^2 + an + b$, where $\lvert a \rvert < 1000$ and $\lvert b \rvert < 1000$

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$.

I've solved the problem using a search through the values of $a$ and $b$, and have improved it by reducing the search space considerably, but I don't understand how the following solution works:

Haskell Code

problem_27 = -(2*a-1)*(a^2-a+41)
    where n = 1000
          m = head $ filter (\x->x^2-x+41>n) [1..]
          a = m-1

Pseudocode

Let n = 1000
Let m = The first i in [1,2..] such that i^2 - i + 41 > n
Let a = m - 1
Then the solution is -(2a - 1)(a^2 - a + 41)