Skip to content

Instantly share code, notes, and snippets.

@timjb
Created July 26, 2011 22:24
Show Gist options
  • Select an option

  • Save timjb/1108231 to your computer and use it in GitHub Desktop.

Select an option

Save timjb/1108231 to your computer and use it in GitHub Desktop.
NineDigitProblem
-- 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