diff --git a/Problem027.hs b/Problem027.hs new file mode 100644 index 0000000..fa97b2f --- /dev/null +++ b/Problem027.hs @@ -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 + ]