Skip to content

Instantly share code, notes, and snippets.

@taiansu
Last active May 12, 2018 11:07
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 taiansu/138e32ee4464d3e28a8d1e9d501efdfa to your computer and use it in GitHub Desktop.
Save taiansu/138e32ee4464d3e28a8d1e9d501efdfa to your computer and use it in GitHub Desktop.
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