Created
September 20, 2010 00:32
-
-
Save jnjosh/587265 to your computer and use it in GitHub Desktop.
Find Lychrel Numbers under 10,000 -- blatantly stolen from several samples
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
reverseNum :: (Integral a) => a -> a | |
reverseNum 0 = 0 | |
reverseNum x = truncate 10 ^ (truncate (logBase 10.0 (fromIntegral x))) * (x `mod` 10) + reverseNum (x `quot` 10) | |
palindrome :: (Integral a) => a -> Bool | |
palindrome x = reverseNum x == x | |
lychrel :: (Integral a) => a -> a -> Bool | |
lychrel _ 100 = True | |
lychrel x y = if palindrome lych then False else lychrel lych (y + 1) | |
where lych = x + (reverseNum x) | |
lychrel' :: (Integral a) => a -> Bool | |
lychrel' x = lychrel x 0 | |
challenge2 = length (filter lychrel' [1..10000]) | |
main :: IO () | |
main = do | |
putStr "Lychrel Numbers < 10,000 = " ; print challenge2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet!!!!