ProjectEular

-- Problem 27

module Main (main) where

import Array
import qualified Common as C (is_prime)

main :: IO ()
main = print $ problem27

problem27 :: Int
problem27 = (fst idx) * (snd idx)
    where
        idx = find_max_idx arr
        arr = listArray ((-999,-999),(999,999))
                        [ num_primes a b | a <- [-999..999], b <- [-999..999] ]

find_max_idx :: Array (Int,Int) Int -> (Int,Int)
find_max_idx arr = find_max_idx_iter arr idcs idx
    where (idx:idcs) = indices arr

find_max_idx_iter :: Array (Int,Int) Int -> [(Int,Int)] -> (Int,Int) -> (Int,Int)
find_max_idx_iter arr idcs max_idx
    | null idcs             = max_idx
    | arr!idx > arr!max_idx = find_max_idx_iter arr idcs' idx
    | otherwise             = find_max_idx_iter arr idcs' max_idx
    where (idx:idcs') = idcs

num_primes :: Int -> Int -> Int
num_primes a b = num_primes_iter 0 a b

num_primes_iter :: Int -> Int -> Int -> Int
num_primes_iter n a b
    | C.is_prime (n^2 + a*n + b) = 1 + (num_primes_iter (n+1) a b)
    | otherwise                  = 0

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS