{-# LANGUAGE BangPatterns #-} import Data.Bits (xor) import Data.List (sort) mex :: [Int] -> Int mex = mexSorted 0 . sort mexSorted :: Int -> [Int] -> Int mexSorted !i xs@(x : xs') | x < i = mexSorted i xs' mexSorted i xs@(x : xs') | x == i = mexSorted (i + 1) xs' mexSorted i _ = i -- |@nimValues !! n@ is the nim-value (aka Sprague-Grundy number) of the game described in -- http://c...content-available-to-author-only...e.com/questions/16228 on the path with n vertices. nimValues :: [Int] nimValues = 0 : 1 : map f [0 ..] where f s = mex $ zipWith xor (take (s `div` 2 + 1) nimValues) (reverse (take (s + 1) nimValues)) -- |Report the values of n (≤ 100) such that the first player loses in the game -- on the path with n vertices (including 0, meaning the empty graph). main :: IO () main = print $ map fst $ filter (\(_, nim) -> nim == 0) $ zip [0 :: Int .. 100] nimValues