Skip to content

Instantly share code, notes, and snippets.

@mstahl
Created December 11, 2008 19:51
Show Gist options
  • Save mstahl/34841 to your computer and use it in GitHub Desktop.
Save mstahl/34841 to your computer and use it in GitHub Desktop.
module Main where
import System
import Data.List
import Primes
primes :: [Integer]
primes = filter (prime) [2..]
concatenations :: Show a => [a] -> [Integer]
concatenations (x:xs) = (foldl (++) [] $ map (pairify x) xs) ++ (concatenations xs)
where pairify x y = map (\s -> (read s)::Integer) [(show x) ++ (show y), (show y) ++ (show x)]
concatenations [] = []
solutions = [(a,b,c,d,e) | a <- drop 4 primes
, b <- takeWhile (<a) (drop 3 primes)
, c <- takeWhile (<b) (drop 2 primes)
, d <- takeWhile (<c) (drop 1 primes)
, e <- takeWhile (<d) primes
, all prime (concatenations [a,b,c,d])]
-- Uncomment below to solve the example problem
-- solutions = [(a,b,c,d) | a <- drop 3 primes
-- , b <- takeWhile (<a) (drop 2 primes)
-- , c <- takeWhile (<b) (drop 1 primes)
-- , d <- takeWhile (<c) primes
-- , all prime (concatenations [a,b,c,d])]
main = do print $ head solutions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment