24 lines
720 B
Haskell
24 lines
720 B
Haskell
-- 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
|
|
]
|