Last active
December 12, 2015 06:19
-
-
Save Wollw/4728688 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
-- Project Euler Problem 36 | |
-- Double-base palindromes | |
-- | |
-- The decimal number, 585 = 1001001001 base 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.) | |
import Data.Digits | |
isBinDecPalindrome :: Integer -> Bool | |
isBinDecPalindrome n = isBinPalindrome n && isDecPalindrome n | |
where | |
isDecPalindrome n = (reverse . show $ n) == show n | |
isBinPalindrome n = (reverse . showBinary $ n) == showBinary n | |
showBinary = foldr (\x s -> (show x) ++ s) [] . digits 2 | |
main = print . sum . filter isBinDecPalindrome $ [1..1000000] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment