Add solution for problem 27.

This commit is contained in:
Jesse D. McDonald 2015-08-08 18:00:12 -05:00
parent a0472905a4
commit cfd8dbdf1d
1 changed files with 23 additions and 0 deletions

23
Problem027.hs Normal file
View File

@ -0,0 +1,23 @@
-- Considering quadratics of the form:
--
-- n² + an + b, where |a| < 1000 and |b| < 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.
import Euler
isPrime :: Integer -> Bool
isPrime n = n >= 2 && not (any (\p -> n `mod` p == 0) $ takeWhile (< n) primes)
nPrimes a b = length $ takeWhile isPrime $ map (\n -> n^2 + a*n + b) [0..]
main = print $ uncurry (*) $ snd $ maximumBy (compare `on` fst) $
[ (n, (a, b))
| a <- [-999..999]
, b <- [-999..999]
, isPrime (40^2 + a*40 + b)
, isPrime (30^2 + a*30 + b)
, let n = nPrimes a b
]