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