Last active
May 15, 2018 09:55
-
-
Save zetavg/b03505c36a692d8bf8be14f48162098f to your computer and use it in GitHub Desktop.
FLOLAC '18 Week of Code #3: Goldbach's conjecture
This file contains hidden or 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
sieve :: [Int] -> [Int] | |
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0] | |
primes :: [Int] | |
primes = sieve [2..] | |
goldbachs :: Int -> [(Int, Int)] | |
goldbachs n = [(x, y) | x <- primesSmallerThenHalfOfN, y <- primesSmallerThenN, x < y, x + y == n] | |
where primesSmallerThenHalfOfN = takeWhile (< n `div` 2) primes | |
primesSmallerThenN = takeWhile (< n) primes | |
goldbach :: Int -> Maybe (Int, Int) | |
goldbach n = case take 1 $ goldbachs n of pair:_ -> Just(pair) | |
otherwise -> Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment