Life Goes On

まあまあだけど楽しんでる方です

36問目

百万未満の数で、2進表記でも10進表記でも回文になるようなものを全て足し合わせる。
showIntAtBaseという関数を知らず、最初は自分で書いてしまいました。

import Data.Char
import Numeric

main = print $ sum $ euler036 999999

euler036 :: Int -> [Int]
euler036 n = filter bothPalindromic [1..n]

bothPalindromic :: Int -> Bool
bothPalindromic n = isPalindrome (showBit n) && isPalindrome (show n)

isPalindrome :: String -> Bool
isPalindrome s = s == reverse s

showBit :: Int -> String
showBit n = showIntAtBase 2 intToDigit n ""
-- showBit 1 = "1"
-- showBit n = (intToDigit $ mod n 2) : (showBit $ div n 2)