Skip to content

Commit

Permalink
euler 27
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Korzhuev committed Apr 20, 2012
1 parent 15ff472 commit 5b55e7b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 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

0 comments on commit 5b55e7b

Please sign in to comment.