isPalindrome :: Integer -> Bool
isPalindrome n = reverse x == x
where x = show n
is3x3 :: Integer -> Bool
is3x3 n = any (\x -> cond1 x && cond2 x) [101..999]
where
cond1 x = n `mod` x == 0
cond2 x = length (show $ n `div` x) == 3
main = print $ head [p | p <- [999^2,999^2-1..], isPalindrome p, is3x3 p]
I have some doubts about the code I wrote:
isPalindrome
: is it ok or is it better to keep dividing by 10, storing the remainder in a list and check if list is equal to the reversed self?is3x3
: is it a good practice to declare functions inside functions? Is there a better way to write the function?- how to delete the parentheses in
cond2
?$
doesn't work because of==
[999^2,999^2-1..]
:- is Haskell smart and calculates 999^2 only once, subtracting 1 every time? Or does it compute 999^2-x for each xth+1 element of the list?
- is there a better way to write this list?
- is it better to put the end of the list (in this case 10001) or it doesn't matter in terms of performance? I know lists are lazy but ...