I have been solving some random Project Euler problems to practice my haskell. After solving the problem, I usually look up the solution on the haskell wiki.
For Problem 27, I solved it the regular way, i.e, using a combination of isPrime and maps. But then, I saw this solution on the wiki. I have no idea how this solution works. The only other mention I find of it is from this closed thread on math stackexchange.
problem_27 :: Integer
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
My attempt at understanding this code
As I understand, this code does the following
- Let n=1000
- Let m be the first natural number x, such that x^2 - x + 41 is greater than n(here 1000)
- Then a = m - 1
- The answer to the problem is -(2a-1) * (a^2-a+41): (I think that this means that the two are the coefficients.)
But I do not understand why this program gives the correct answer. Or in other words, What is the reasoning behind this algorithm?