I love this kind of beautiful equations, and I can’t resist the opportunity for writing some Haskell.
This code prints out some cute multiplication palindromes.
import Data.List
import Data.Function
import Control.Monad
type Pair = (Int,Int)
rev :: Int -> Int
rev = foldl go 0 . digits
where go m n = 10 * m + n
digits :: Int -> [Int]
digits = unfoldr uncons
where uncons m = guard (m > 0) >> return (m `mod` 10, m `div` 10)
mp :: Pair -> Bool
mp (x,y) = x * y == rev y * rev x
comb :: [a] -> Int -> [[a]]
comb _ 0 = [[]]
comb [] _ = []
comb (a:as) n = map (a:) (comb as (n-1)) ++ comb as n
nums :: [Int]
nums = concat [ [10*a + b, 10*b + a] | [a,b] <- comb [1..9] 2 ]
pairs :: [Pair]
pairs = nubBy ((==) `on` uncurry (*)) [ (a,b) | [a,b] <- replicateM 2 nums, a /= rev b, mp (a,b) ]
main :: IO ()
main = forM_ pairs write
where write (a,b) = putStrLn $ mult (a,b) ++ " = " ++ mult (rev b, rev a)
mult (x,y) = show x ++ " × " ++ show y
Output:
12 × 42 = 24 × 21
12 × 63 = 36 × 21
12 × 84 = 48 × 21
13 × 62 = 26 × 31
13 × 93 = 39 × 31
14 × 82 = 28 × 41
23 × 64 = 46 × 32
23 × 96 = 69 × 32
24 × 63 = 36 × 42
24 × 84 = 48 × 42
26 × 93 = 39 × 62
34 × 86 = 68 × 43
36 × 84 = 48 × 63
46 × 96 = 69 × 64