Last active
February 24, 2018 21:57
-
-
Save ckoparkar/f0d8c8a68456101e9cc51513ad63b478 to your computer and use it in GitHub Desktop.
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
module Blade where | |
import qualified Data.Set as S | |
-- Implemented the inclusion-exclusion algorithm: | |
-- <https://stackoverflow.com/a/27248971> | |
-- (∑ Aj) in the formula | |
-- These all the numbers we've counted more than once | |
doubleCounts :: [Int] | |
doubleCounts = S.toList $ S.map slcm subsets | |
where | |
subsets :: S.Set (S.Set Int) | |
subsets = S.filter (not . (== 1) . S.size) $ | |
S.filter (not . S.null) $ | |
S.filter (\s -> S.isProperSubsetOf s caterpillars) $ | |
S.powerSet caterpillars | |
-------------------------------------------------------------------------------- | |
slcm :: S.Set Int -> Int | |
slcm = S.foldr lcm 1 | |
caterpillars :: S.Set Int | |
caterpillars = S.fromList [2,3,5,7,11,13] | |
limit :: Int | |
limit = 2 * (10 ^ 9) | |
main = limit - (a + b - c) | |
where | |
a = sum $ S.map (quot limit) caterpillars | |
b = quot limit $ slcm caterpillars | |
c = sum $ map (quot limit) doubleCounts | |
-- 931468558 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment