0004.hs 908 B

12345678910111213141516171819202122232425262728
  1. {-
  2. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.
  3. Find the largest palindrome made from the product of two 3-digit numbers.
  4. -}
  5. rev_list :: [Char] -> [Char]
  6. rev_list [] = []
  7. rev_list [x] = [x]
  8. rev_list (x:xs) = (rev_list xs) ++ [x]
  9. bigger :: Int -> Int -> Int
  10. bigger x y = if (x > y) then x else y
  11. is_palindrome :: Int -> Bool
  12. is_palindrome x = (xs == (rev_list xs)) where xs = show x
  13. solution' :: Int -> Int -> Int -> Int
  14. solution' x 1000 res = res
  15. solution' 1000 y res = solution' 100 (y + 1) res
  16. solution' x y res = solution' (x + 1) y $! (if (is_palindrome (x * y))
  17. then (bigger (x * y) res)
  18. else res)
  19. solution :: Int
  20. solution = solution' 100 100 0
  21. main :: IO ()
  22. main = putStrLn ("Solution: " ++ show solution)