Skip to content

Instantly share code, notes, and snippets.

@Wollw
Last active December 12, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wollw/4728688 to your computer and use it in GitHub Desktop.
Save Wollw/4728688 to your computer and use it in GitHub Desktop.
-- 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