Created
June 20, 2017 15:00
-
-
Save matsubara0507/2787f3563ff855093e18cd68027be3c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import Data.List (unfoldr) | |
| import Data.Tuple (swap) | |
| solve :: Int -> Int | |
| solve n = head $ filter isMultiPalidromic [n..] | |
| isMultiPalidromic :: Int -> Bool | |
| isMultiPalidromic n = countTwo . filter isPalidromic' $ map (`convertRadix` n) [2..n] | |
| isPalidromic :: [Int] -> Bool | |
| isPalidromic = (==) <*> reverse | |
| isPalidromic' :: [Int] -> Bool | |
| isPalidromic' ns = length ns > 2 && isPalidromic ns | |
| convertRadix :: Int -> Int -> [Int] | |
| convertRadix n = reverse . unfoldr (fmap (swap . (`divMod` n)) . boolToMaybe (== 0)) | |
| boolToMaybe p a = if p a then Nothing else Just a | |
| countTwo :: [a] -> Bool | |
| countTwo (x1 : x2 : xs) = True | |
| countTwo _ = False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment