Last active
December 10, 2015 01:58
-
-
Save felher/4363849 to your computer and use it in GitHub Desktop.
puzzle
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
type MT = (Int, Int) | |
allTuples :: [MT] | |
allTuples = [ (x, y) | x <- [2 .. 97], y <- [2 .. 97], x >= y, x + y < 100] | |
isPrime :: Int -> Bool | |
isPrime x = null [d | d <- [2 .. x - 1], x `rem` d == 0] | |
primeTuples :: [MT] | |
primeTuples = filter isPrimeTuple allTuples where | |
isPrimeTuple (x, y) = isPrime x && isPrime y | |
sortOutPrimeTuples :: [MT] -> [MT] | |
sortOutPrimeTuples list = filter (not . (`elem` primeTuples)) list where | |
sortOutTuplesWhereSumCanBeExpressedAsSumOfTwoPrimes :: [MT] -> [MT] | |
sortOutTuplesWhereSumCanBeExpressedAsSumOfTwoPrimes list = filter canNotBeExpressed list where | |
canNotBeExpressed (x, y) = null [ (a, b) | (a, b) <- primeTuples, a + b == x + y] | |
sortOutTuplesWhereProductIsAmbiguous :: [MT] -> [MT] | |
sortOutTuplesWhereProductIsAmbiguous list = filter isNotAmbiguous list where | |
isNotAmbiguous (x, y) = 1 == length [ (a, b) | (a, b) <- list, a * b == x * y] | |
sortOutTuplesWhereSumIsAmbiguous :: [MT] -> [MT] | |
sortOutTuplesWhereSumIsAmbiguous list = filter isNotAmbiguous list where | |
isNotAmbiguous (x, y) = 1 == length [ (a, b) | (a, b) <- list, a + b == x + y] | |
sortOutTuplesWhereSumIsNotAmbiguous :: [MT] -> [MT] | |
sortOutTuplesWhereSumIsNotAmbiguous list = filter isNotAmbiguous list where | |
isNotAmbiguous (x, y) = 1 < length [ (a, b) | (a, b) <- list, a + b == x + y] | |
solve :: [MT] -> [MT] | |
solve = sortOutTuplesWhereSumIsAmbiguous | |
. sortOutTuplesWhereProductIsAmbiguous | |
. sortOutTuplesWhereSumIsNotAmbiguous | |
. sortOutTuplesWhereSumCanBeExpressedAsSumOfTwoPrimes | |
. sortOutPrimeTuples | |
main = mapM_ (putStrLn . show) $ solve allTuples |
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
sub all-tuples() { | |
for 2 .. 97 -> \a { for a .. 99 - a -> \b { [a, b] } } | |
} | |
sub classifyBy(&code, @tuples) { | |
@tuples.classify: -> [\a, \b] { code a, b } | |
} | |
sub not-sum-of-primes([\a, \b]) { | |
state %prim-sums = do all-tuples() ==> grep { .all.is-prime } ==> classifyBy * + *; | |
not %prim-sums{a + b}; | |
} | |
sub filterWith(&classifier, &test, @numbers) { | |
my %count = do @numbers ==> classifyBy &classifier; | |
@numbers ==> grep -> [\a, \b] { test %count{classifier a, b} }; | |
} | |
all-tuples() | |
==> grep { not .all.is-prime } #P: I can not find them | |
==> grep ¬-sum-of-primes #S: I knew you could not find them | |
==> filterWith * + *, * > 1 #S: I could not find them either | |
==> filterWith * * *, * == 1 #P: Now I found them | |
==> filterWith * + *, * == 1 #S: Now I found them too | |
==> say; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment