Skip to content

Instantly share code, notes, and snippets.

@wrhall
Created August 17, 2014 17:22
Show Gist options
  • Save wrhall/01ecf57fb59eb78b082f to your computer and use it in GitHub Desktop.
Save wrhall/01ecf57fb59eb78b082f to your computer and use it in GitHub Desktop.
A couple solutions to project euler 49.
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