Created
July 26, 2011 22:24
-
-
Save timjb/1108231 to your computer and use it in GitHub Desktop.
NineDigitProblem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- http://csokavar.hu/blog/2010/04/20/problem-of-the-week-9-digit-problem/ | |
| import Data.List (delete) | |
| main :: IO () | |
| main = print $ solve 0 1 [1..9] | |
| solve :: Integral a => a -> a -> [a] -> [a] | |
| solve curr _ [] = [curr] | |
| solve curr divisor possibilities = concatMap solveFor possibilities | |
| where solveFor n = let curr' = curr `appendDigit` n | |
| in if curr' `divisibleBy` divisor | |
| then solve curr' (divisor+1) (delete n possibilities) | |
| else [] | |
| appendDigit a b = (a*10) + b | |
| divisibleBy a b = a `mod` b == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment