[Haskell-beginners] Project euler question

martin martin.drautzburg at web.de
Wed May 21 20:09:24 UTC 2014


Hello all,

I tried to solve Problem 24 (https://projecteuler.net/problem=24) and came up with the following solution:

import Data.List.Ordered
import Data.Char

elems = [0,1,2,3,4,5,6,7,8,9] :: [Int]

x = do
    a <- elems
    b <- elems `without` [a]
    c <- elems `without` [a,b]
    d <- elems `without` [a,b,c]
    e <- elems `without` [a,b,c,d]
    f <- elems `without` [a,b,c,d,e]
    g <- elems `without` [a,b,c,d,e,f]
    h <- elems `without` [a,b,c,d,e,f,g]
    i <- elems `without` [a,b,c,d,e,f,g,h]
    j <- elems `without` [a,b,c,d,e,f,g,h,i]
    return [a,b,c,d,e,f,g,h,i,j]

without a b = minus a ( sort b)

solution = filter isDigit $ show $ (x !! 1000001)
-- "2783915640"

PE tells me that this is wrong, and I peeked the correct answer, which is 2783915460 (the 4 and 6 are swapped). So I
tried to find out where the correct answer is in my list x and added

y = filter (\(x,y) -> x == "2783915460") $ zip (map (filter isDigit . show) x) [1..]
-- [("2783915460",1000000)]

How can that be? "solution" tells me that the millionth element is "2783915640" but "y" tells me that "2783915460" is at
the millionth position? I just cannot see it.



More information about the Beginners mailing list