Created
November 28, 2011 18:05
-
-
Save dmalikov/1401344 to your computer and use it in GitHub Desktop.
Project Euler 118 (4m 46s)
This file contains 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
import Data.List.Split (splitPlaces) | |
import Data.List (permutations, sort, nub) | |
import Data.Numbers.Primes | |
import Data.Bits | |
import Data.Digits | |
import Data.Char (digitToInt) | |
listToNum :: [Int] -> Integer | |
listToNum = toInteger . foldl1 ((+).(*10)) | |
numToList :: Integer -> [Int] | |
numToList = map digitToInt . show | |
splits :: [Int] -> [[[Int]]] | |
splits list = [list] : map (flip splitPlaces list . toLengths v) [1..v] | |
where v = 2 ^ (length list - 1) - 1 | |
toLengths :: Int -> Int -> [Int] | |
toLengths m x = helper 1 0 [] | |
where | |
helper :: Int -> Int -> [Int] -> [Int] | |
helper t l ls | |
| m < t = l+1 : ls | |
| x .&. t == t = helper (t*2) 0 (l+1 : ls) | |
| otherwise = helper (t*2) (l+1) ls | |
main = print . length . nub . map sort . concat . filter (not . null) . map ( filter (all isPrime) . map (map listToNum) . splits ) . permutations $ [1..9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment