Skip to content

Instantly share code, notes, and snippets.

@JustusAdam
Created August 30, 2015 17:59
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 JustusAdam/a8a63cd99cf3fd1c4207 to your computer and use it in GitHub Desktop.
Save JustusAdam/a8a63cd99cf3fd1c4207 to your computer and use it in GitHub Desktop.
module Lychrel where
import System.Environment
import Data.Foldable
iterateUntil f v =
maybe
[]
((:) <$> id <*> iterateUntil f)
(f v)
reverseInt n = aux n 0
where
aux 0 y = y
aux x y =
let
(x',y') = x `quotRem` 10
in
aux x' (10*y+y')
reverseAndAdd x =
if x == x'
then Nothing
else return (x + x')
where
x' = reverseInt x
main = do
[number] <- getArgs
let l = iterateUntil reverseAndAdd $ read number
traverse_ print l
putStrLn $ "Palindrome found in " ++ show (length l) ++ " steps"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment