ITコンサルの日常

ITコンサル会社に勤務する普通のITエンジニアの日常です。

abc * de = fghijな0〜9

上の問題を一般化してみた。(固定な4をやめてみた)
ついでに頭0禁止にしてみた。

import List

main = putStr $ unlines $ map format $ filter test $ perm [0..9]

perm :: [Int] -> [[Int]]
perm [] = [[]]
perm xs = concat [map (x:) $ perm (delete x xs) | x <- xs]

test :: [Int] -> Bool
test [a,b,c,d,e,f,g,h,i,j] = if (a*100 + b*10 + c) * (d*10 + e) == (f*10000 + g*
1000 + h*100 + i*10 + j) && f /= 0 then True else False

format :: [Int] -> String
format [a,b,c,d,e,f,g,h,i,j] = listToString [a,b,c] ++ " x " ++ listToString[d,e
] ++ " = " ++ listToString [f,g,h,i,j]

listToString :: (Show a) => [a] -> String
listToString l = concatMap show l

結果はこう。

297 x 54 = 16038
345 x 78 = 26910
367 x 52 = 19084
396 x 45 = 17820
402 x 39 = 15678
495 x 36 = 17820
594 x 27 = 16038
715 x 46 = 32890
927 x 63 = 58401

固定する数字を2, 5, 7, 9のいずれかにしておけば、別解の無い良問となったでしょうね。