Vinicius Teshima 5 месяцев назад
Родитель
Сommit
74e8493992
1 измененных файлов с 29 добавлено и 0 удалено
  1. 29 0
      haskell/src/0004.hs

+ 29 - 0
haskell/src/0004.hs

@@ -0,0 +1,29 @@
+{-
+A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.
+Find the largest palindrome made from the product of two 3-digit numbers.
+-}
+
+rev_list :: [Char] -> [Char]
+rev_list []     = []
+rev_list [x]    = [x]
+rev_list (x:xs) = (rev_list xs) ++ [x]
+
+bigger :: Int -> Int -> Int
+bigger x y = if (x > y) then x else y
+
+is_palindrome :: Int -> Bool
+is_palindrome x = (xs == (rev_list xs)) where xs = show x
+
+solution' :: Int -> Int -> Int -> Int
+solution' x    1000 res = res
+solution' 1000 y    res = solution' 100 (y + 1) res
+solution' x y res = solution' (x + 1) y $! (if (is_palindrome (x * y))
+                                            then (bigger (x * y) res)
+                                            else res)
+
+solution :: Int
+solution = solution' 100 100 0
+
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)