Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Korzhuev
committed
Apr 20, 2012
1 parent
15ff472
commit 5b55e7b
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |