Skip to content

Instantly share code, notes, and snippets.

@matsubara0507
Created June 20, 2017 15:00
Show Gist options
  • Select an option

  • Save matsubara0507/2787f3563ff855093e18cd68027be3c9 to your computer and use it in GitHub Desktop.

Select an option

Save matsubara0507/2787f3563ff855093e18cd68027be3c9 to your computer and use it in GitHub Desktop.
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