Skip to content

Instantly share code, notes, and snippets.

@dmalikov
Created December 28, 2011 20:01
Show Gist options
  • Save dmalikov/1529446 to your computer and use it in GitHub Desktop.
Save dmalikov/1529446 to your computer and use it in GitHub Desktop.
Project Euler 78 (14s)
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