From 5b55e7b241d93380ae0637737734d6651afcf170 Mon Sep 17 00:00:00 2001 From: Andrew Korzhuev Date: Fri, 20 Apr 2012 14:26:56 +0400 Subject: [PATCH] euler 27 --- euler/27.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 euler/27.hs diff --git a/euler/27.hs b/euler/27.hs new file mode 100644 index 0000000..7465dd3 --- /dev/null +++ b/euler/27.hs @@ -0,0 +1,35 @@ +-- 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 Data.List (maximumBy) +import Data.Ord (comparing) +import Data.Array (listArray) +import Data.Maybe (Maybe (Nothing, Just)) +import Numerix (primes, bisect) + +primeArr = listArray (0, len - 1) x + where + x = takeWhile (< 10^4) primes + len = length x + +isPrime = toBool . bisect primeArr + where + toBool (Just _) = True + toBool Nothing = False + +euler :: Int -> Int -> [Int] +euler a b = map (\n -> n^2 + a*n + b) [0..] + +eulers = [(a * b, euler a b) | a <- [-999..999], b <- [-999..999]] + +match = length . takeWhile isPrime + +answer = maximumBy (comparing (match.snd)) eulers + +main = print $ fst answer \ No newline at end of file