Skip to content

Instantly share code, notes, and snippets.

@zetavg
Last active May 15, 2018
Embed
What would you like to do?
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