Created
December 28, 2011 20:01
-
-
Save dmalikov/1529446 to your computer and use it in GitHub Desktop.
Project Euler 78 (14s)
This file contains hidden or 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
import Data.Array | |
pentagonals :: [Int] | |
pentagonals = [ n*(3*n-1) `div` 2 | n <- pentagonalIndices ] | |
where | |
pentagonalIndices :: [Int] | |
pentagonalIndices = concatMap (\x -> [x,-x]) [1..] | |
partition :: Array Int Integer | |
partition = listArray (0, 100000) $ 1 : 1 : map partition' [2..] | |
where | |
partition' n = sum (zipWith ($) (cycle [(partition !), (partition !), negate . (!) partition, negate . (!) partition]) $ indices n) | |
indices :: Int -> [Int] | |
indices n = takeWhile (>= 0) [n - x | x <- pentagonals] | |
main = | |
print . fst . head . filter (\x -> snd x `mod` 1000000 == 0) . assocs $ partition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment