3 van 3

Re: Programmeerprobleem

Geplaatst: za 28 apr 2007, 01:52
door EvilBro
Bij gebrek aan beter lever ik maar mijn eigen Haskell versie...

Code: Selecteer alles

module Puzzle

  where



split' 0 = []

split' x = (mod x 10) : (split' (div x 10))

split x = reverse (split' x)



combine' [] = 0

combine' (x:xs) = x + (10 * combine' xs)

combine x = combine' (reverse x)



noZeros [] = True

noZeros (x:xs) = (x /= 0) && (noZeros xs)



noDuplicates [] = True

noDuplicates (x:xs) =  (length (filter (\y -> y == x) xs) == 0) && (noDuplicates xs)



isPrime :: Int -> Bool

isPrime x | x < 2 = False

		  | x >= 2 = length (filter (\y -> (mod x y) == 0) [2..(x-1)]) == 0



checkDigits (a:b:c:d:e:rest) = (isPrime a) && 

							   (b == e - a) && 

							   (c == 2 * a) &&

							   (d == c + 3) &&

							   (e == abs (a - d))



checkRange x = map (combine) (filter (checkDigits) (filter (noDuplicates) (filter (noZeros) (map (split) x))))



solveIt = do

  checkRange [10000..99999]

Re: Programmeerprobleem

Geplaatst: zo 29 apr 2007, 08:39
door jhnbk
mooi zo :(