Skip to content

Instantly share code, notes, and snippets.

@zaneli
Last active August 29, 2015 14:13
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 zaneli/b1502eaa810d5ce21cba to your computer and use it in GitHub Desktop.
Save zaneli/b1502eaa810d5ce21cba to your computer and use it in GitHub Desktop.
「HaskellでProject Euler(Problem 55~57)」ブログ用
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
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
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
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