Project Euler Problem 078
import Control.Monad.ST import Data.List import qualified Data.Array.Unboxed as U import Data.Array.ST dpf :: Int -> U.UArray Int Int dpf n = runSTUArray ( do{ a <- (newArray (0,n) 0) :: ST s (STUArray s Int Int); writeArray a 0 1; sequence_ [ do{sa <- sequence $ [fmap (*sign)$ readArray a (i-p) | (p,sign) <- takeWhile((<=i).fst) pentaa1]; sb <- sequence $ [fmap (*sign)$ readArray a (i-p) | (p,sign) <- takeWhile((<=i).fst) pentaa2]; writeArray a i ((sum sa+sum sb)`mod` (10^(6::Integer)))} | i<-[1..n]]; return a}) where pentaa1 = zip (scanl1(+) [1,4..]) (cycle [1,-1]) pentaa2 = zip (scanl1(+) [2,5..]) (cycle [1,-1]) ans1 :: Maybe (Int, Int) ans1 = find ((==0).snd) $ U.assocs $ dpf 100000 main :: IO () main = print ans1