Skip to content

Instantly share code, notes, and snippets.

@dmalikov
Created November 28, 2011 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmalikov/1401344 to your computer and use it in GitHub Desktop.
Save dmalikov/1401344 to your computer and use it in GitHub Desktop.
Project Euler 118 (4m 46s)
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