Last active
August 29, 2015 14:22
-
-
Save alcol80/0d218b2a28591fa23937 to your computer and use it in GitHub Desktop.
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
-- Nell'atrio di una scuola ci sono 100 armadietti. | |
-- Di notte il custode li apre tutti. | |
-- Poi fa una seconda passata e ne chiude uno ogni due. | |
-- Poi fa una terza passata e ogni tre lucchetti, lo apre o chiude a seconda se sia chiuso o aperto. | |
-- Poi una quarta passata, uno ogni quattro. | |
-- Poi una quinta e così via. | |
-- Dopo la centesima passata, quanti sono i lucchetti chiusi e quanti quelli aperti? | |
module Main where | |
oneRound k = zipWith touch [0..] -- apre/chiude un armadietto ogni k | |
where touch i | i `mod` k == 0 = not | |
| otherwise = id | |
nightRounds n = foldl (flip oneRound) (take n $ repeat False) [1..n] | |
main :: IO () | |
main = do | |
let n = 100 -- numero degli armadietti (numerazione parte da 0) | |
let armadietti = nightRounds n | |
putStrLn $ show $ [c | (c, open) <- zip [0..] armadietti, open] | |
-- putStrLn $ show $ map fst $ filter snd $ zip [0..] armadietti | |
-- >>> Output: | |
-- [1,4,9,16,25,36,49,64,81] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment