Skip to content

Instantly share code, notes, and snippets.

@ckoparkar
Last active February 24, 2018 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckoparkar/f0d8c8a68456101e9cc51513ad63b478 to your computer and use it in GitHub Desktop.
Save ckoparkar/f0d8c8a68456101e9cc51513ad63b478 to your computer and use it in GitHub Desktop.
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