Created
May 13, 2012 17:54
-
-
Save davorb/2689490 to your computer and use it in GitHub Desktop.
chapter1-dropbox.hs
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
{- | |
Chapter 1 | |
1. The product of the first two digits is 24. | |
2. The fourth digit is half of the second digit. | |
3. The sum of the last two digits is the same as the sum of the first and third digits. | |
4. The sum of all the digits is 26. | |
5. The second and third digits add up to the last digit. | |
-} | |
import Char (digitToInt) | |
intAt n num | |
| n >= length (show num) = 0 | |
| otherwise = fromIntegral $ digitToInt ((show num) !! n) | |
rule1 n = intAt 0 n * intAt 1 n == 24 | |
rule2 n = intAt 3 n * 2 == intAt 1 n | |
rule3 n = intAt 3 n + intAt 4 n == intAt 0 n + intAt 2 n | |
rule4 n = sum (map digitToInt (show n)) == 26 | |
rule5 n = intAt 1 n + intAt 2 n == intAt 4 n | |
test input = rule1 input && rule2 input && rule3 input && rule4 input && rule5 input | |
answer = [x | x <- [0..100000], test x] | |
main = do | |
putStrLn $ show answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment