Skip to content

Instantly share code, notes, and snippets.

@zetavg
Last active May 15, 2018 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zetavg/b03505c36a692d8bf8be14f48162098f to your computer and use it in GitHub Desktop.
Save zetavg/b03505c36a692d8bf8be14f48162098f to your computer and use it in GitHub Desktop.
FLOLAC '18 Week of Code #3: Goldbach's conjecture
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