Created
August 17, 2014 17:22
-
-
Save wrhall/01ecf57fb59eb78b082f to your computer and use it in GitHub Desktop.
A couple solutions to project euler 49.
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 qualified Data.Set as Set | |
import qualified Data.Numbers.Primes as Primes | |
import Data.List (sort) | |
import Control.Monad | |
arePerms :: (Integral a, Show a) => a -> a -> Bool | |
arePerms a b = ((sort $ show a) == (sort $ show b)) | |
fourDigitPrimes :: [Integer] | |
fourDigitPrimes = takeWhile (<10000) $ dropWhile (<1000) Primes.primes | |
primePerms :: [(Integer, Integer, Integer)] | |
primePerms = [(x, y, z) | x <- fourDigitPrimes, | |
y <- (takeWhile (< x + 5000) (dropWhile (<= x) fourDigitPrimes)), | |
z <- [y + y - x], | |
arePerms x y, | |
arePerms x z, | |
Primes.isPrime z] | |
primePermsCM :: [(Integer, Integer, Integer)] | |
primePermsCM = do | |
x <- fourDigitPrimes | |
y <- (takeWhile (< x + 5000) (dropWhile (<= x) fourDigitPrimes)) | |
guard $ arePerms x y | |
let z = y + y - x | |
guard $ arePerms x z | |
guard $ Primes.isPrime z | |
return (x, y, z) | |
nicePrint :: (Show a, Show a1, Show a2) => (a, a1, a2) -> [Char] | |
nicePrint (x, y, z) = (show x) ++ (show y) ++ (show z) | |
main :: IO () | |
main = print $ (nicePrint (primePerms !! 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment