New Scientist - Enigma 1557
New Scientist Enigma Number 1557, brute force solution in Haskell. (I apologize for the quality of the code)
Question:
1. What is the four-digit integer divisible by 11 and 13 whose reverse is a larger integer also divisible by 11 and 13?
2. What is the four-digit integer divisible by 17 and 19 whose reverse is a larger integer also divisible by 17 and 19?
--reverses anything that supports read and show reversea :: (Show a, Read a) => a -> a reversea a = read ( reverse ( show a ) ) --takes a list of nums and checks if a int is divisible by all of them isDiv :: [Int] -> Int -> Bool isDiv [] n = True isDiv (x:xs) n | mod n x == 0 = isDiv xs n | otherwise = False --is n divisable by everything in xs? --is the reverse of n also divisable by everything is xs? --is the reverse of n greater than n? solve :: [Int] -> Int -> Maybe Int solve xs n | isCorrect xs n = Just n | otherwise = Nothing where rn = reversea n isCorrect :: [Int] -> Int -> Bool isCorrect xs n = ( isDiv xs n ) && ( isDiv xs rn ) && ( n < rn ) --return the first value that isnt nothing first :: (Eq a) => (a -> Maybe a) -> [a] -> a first f (x:xs) | f x == Nothing = first f xs | otherwise = x solve1 = first (solve [11,13]) [1000..] solve2 = first (solve [17,19]) [1000..]