Last active
August 29, 2015 14:13
-
-
Save zaneli/b1502eaa810d5ce21cba to your computer and use it in GitHub Desktop.
「HaskellでProject Euler(Problem 55~57)」ブログ用
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
main = print $ length $ filter isLychrel [1..9999] | |
isLychrel :: Integer -> Bool | |
isLychrel = not . any isPalindrome . take 49 . tail . iterate addReverseNum | |
addReverseNum :: (Show a, Read a, Num a) => a -> a | |
addReverseNum n = n + reverseNum n | |
where reverseNum = read . reverse . show | |
isPalindrome :: Show a => a -> Bool | |
isPalindrome n = let s = show n in s == reverse s |
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
import Data.Char (digitToInt) | |
main = print $ maximum [digitSum a b | a <- [1..100], b <- [1..100]] | |
digitSum :: (Show a, Num a, Integral b) => a -> b -> Int | |
digitSum n m = sum $ map digitToInt $ show $ n ^ m |
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
import Data.Ratio ((%), denominator, numerator, Ratio) | |
limit = 1000 | |
main = print $ length $ filter (\n -> size (numerator n) > size (denominator n)) $ take limit $ sqrt2 | |
where size = length . show | |
sqrt2 :: [Ratio Integer] | |
sqrt2 = map (1 +) $ iterate (\n -> reciprocal (2 + n)) (1 % 2) | |
reciprocal :: Integral a => Ratio a -> Ratio a | |
reciprocal n = denominator n % numerator n |
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
import Data.Function (on) | |
import Data.Ratio ((%), denominator, numerator, Ratio) | |
limit = 1000 | |
main = (print . length . filter (\n -> (numerator n) `exceed` (denominator n)) . take limit) sqrt2 | |
where exceed = (>) `on` size | |
where size = length . show | |
sqrt2 :: [Ratio Integer] | |
sqrt2 = iterate (\n -> 1 + 1 / (1 + n)) (3 % 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment