Created
May 11, 2018 15:57
-
-
Save jc99kuo/339fd27d50ced55b19a062aa680b006b to your computer and use it in GitHub Desktop.
FLOLAC 2018 Week 3 (2018-5-11 11:46) -- Goldbach's Conjecture
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
-- FLOLAC 2018 Week 3 -- Goldbach's conjecture | |
-- A simple, intuitive, straight fowward solution. | |
-- And may be not so efficient. :-) | |
goldbach :: Int -> Maybe (Int, Int) | |
goldbach nn | |
| nn < 3 = Nothing | |
| odd nn = Nothing | |
| otherwise = | |
if null pairs | |
then Nothing | |
else Just $ head pairs | |
where | |
pairs = [ (m,n) | m <- tryRange, | |
n <- tryRange, | |
(m + n) == nn ] | |
tryRange = takeWhile (\x -> x < nn) primeList | |
primeList = ppList [2 .. ] | |
ppList (n : ns) = n : ppList (filter (\x -> (mod x n) /= 0 ) ns) | |
-- Thanks to the call-by-need lazy evaluation, | |
-- The efficiency is really not bad. :-) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment