Euler problem 27

Haskell

------------------------------

primes = 2 : filter (null . tail . primeFactors) [3,5..]

primeFactors n = factor n primes
                 where
                   factor n (p:ps)
                       | p*p > n = [n]
                       | n `mod` p == 0 = p : factor (n `div` p) (p:ps)
                       | otherwise = factor n ps

list_max (x:xs) = lmf x xs
    where
      lmf x []                    = x
      lmf x xs | (x >= (head xs)) = lmf x (tail xs)
               | otherwise        = lmf (head xs) (tail xs)

elemInd y [] = -1
elemInd y (x:xs) = ei 0 y (x:xs)
                    where
                      ei n y (x:xs)
                          | (x == y)  = n
                          | (null xs) = -1
                          | otherwise = ei (n+1) y xs
                                   

b_list = takeWhile (<= 1000) primes

isPrime x | x <= 1    = False
          | otherwise = null $ tail (primeFactors x)

cPrimeLen [a, b] = [m, a, b]
    where m = length $ takeWhile (== True) $ map (isPrime . (\n -> n^2 + a*n + b)) [0..]

p27_list = [cPrimeLen [a, b] | a <- [-n..n], b <- b_list]    where n = 1000

p27_n = map head p27_list

p27 = p27_list !! (elemInd (list_max p27_n) p27_n)

-- 71, a == -61, b == 971

------------------------------

— Me@2015-06-19 10:01:22 PM

2015.06.20 Saturday (c) All rights reserved by ACHK