19 lines
660 B
Haskell
19 lines
660 B
Haskell
-- The decimal number, 585 = 1001001001_2 (binary), is palindromic in both bases.
|
|
--
|
|
-- Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
|
|
--
|
|
-- (Please note that the palindromic number, in either base, may not include leading zeros.)
|
|
|
|
toBinary :: Int -> [Int]
|
|
toBinary 0 = []
|
|
toBinary 1 = [1]
|
|
toBinary n = toBinary (n `div` 2) ++ [n `mod` 2]
|
|
|
|
doublePalindrome :: Int -> Bool
|
|
doublePalindrome n = show n == reverse (show n) && toBinary n == reverse (toBinary n)
|
|
|
|
main = do
|
|
let result = sum $ filter doublePalindrome [1..999999]
|
|
putStrLn $ "Project Euler, Problem 36\n"
|
|
++ "Answer: " ++ show result
|