Last active
July 25, 2019 15:46
-
-
Save oxidist/6b5f9367d1bba3df7f948738fee8f0c3 to your computer and use it in GitHub Desktop.
Project Euler 78
This file contains 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 (Array, array, (!), assocs) | |
import Data.List (transpose, foldl') | |
interleave :: [a] -> [a] -> [a] | |
interleave xs ys = concat . transpose $ [xs, ys] | |
pent :: [Int] -> [Int] | |
pent = map (\x -> div (3*x^2 - x) 2) | |
pentagonals :: [Int] | |
pentagonals = interleave (pent [1..195]) (pent [-1,-2.. (-195)]) | |
cache :: Array Int Int | |
cache = array (0, 57000) [(x, partition x `mod` 1000000) | x <- [0..57000]] | |
partition :: Int -> Int | |
partition n | |
| n <= 1 = 1 | |
| otherwise = foldl' (+) 0 [s * cache ! (n - p) | (s, p) <- zip (cycle [1, 1, -1, -1]) $ takeWhile (<= n) pentagonals] | |
main :: IO () | |
main = print $ head [i | (i, p) <- assocs cache, p == 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment