Skip to content

Instantly share code, notes, and snippets.

@taiansu
Last active May 12, 2018
Embed
What would you like to do?
Flolac week of code 3: goldbach
goldbach :: Int -> Maybe(Int, Int)
goldbach n = search halfOfPrimes where
search [] = Nothing
search (x:xs) = if isPrime (n - x) then Just (x, n - x) else search xs
halfOfPrimes = 2 : [ x | x <- [3, 5..half], isPrime x]
half = div n 2
isPrime :: Int -> Bool
isPrime 2 = True
isPrime n = not $ any isFactor (2 : [3, 5 .. (square n)])
where square = floor . sqrt . fromIntegral
isFactor x = mod n x == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment