Skip to content

Instantly share code, notes, and snippets.

@jnjosh
Created September 20, 2010 00:32
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 jnjosh/587265 to your computer and use it in GitHub Desktop.
Save jnjosh/587265 to your computer and use it in GitHub Desktop.
Find Lychrel Numbers under 10,000 -- blatantly stolen from several samples
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
@ashumz
Copy link

ashumz commented Sep 20, 2010

Sweet!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment