1

I am doing problem 68 at project euler and came up with the following code in Haskell to return the list of numbers which fit the (given) solution:

lists = [n|n<- permutations [1..6] , ring n ] 
ring [a,b,c,d,e,f] = (length $ nub $ map sum [[d,c,b],[f,b,a],[e,a,c]]) == 1

This only returns a list of lists of 6 numbers each which fit the solution. What I don't know how to do, is make it return the actual solution, the lists that fit the form:

[d,c,b],[f,b,a],[e,a,c]

How can I make lists return a list of this format?

(PS: I will add in the appropriate functions to return what the site actually wants later)

4

1 に答える 1

2

It's simply

lists = [ [[d,c,b],[f,b,a],[e,a,c]] | n@[a,b,c,d,e,f] <- permutations [1..6], ring n ] 

Or in order to generate the strings:

[ foldl (++) "" $ map show [d,c,b,f,b,a,e,a,c] | n@[a,b,c,d,e,f] <- permutations [1..6], ring n ] 
于 2009-11-22T16:34:17.137 に答える