Created
August 30, 2015 17:59
-
-
Save JustusAdam/a8a63cd99cf3fd1c4207 to your computer and use it in GitHub Desktop.
This file contains 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
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